0

I have a use case where I want to use scala list and java list in the same file in scala codebase. I am using java list by importing java.util.list. Now If I want to use the scala list in the same file, it's taking that list as java list and throws an error.

2 Answers 2

4

You can use the full qualified name of the list like:

scala.collection.immutable.List

or rename it when import like:

import scala.collection.immutable.{List=> ScalaList}

3

You can use Fully Qualified Name (FQN)

  • Java List is java.utils.List
  • Scala List is scala.collection.immutable.List

You also use alias:

import scala.collection.immutable.{List=>ScalaList}
import java.utils.{List=>JavaList}

Now you can refer to them as ScalaList and JavaList.

1
  • @itisha minor note that, in general, the convention is to just import the Java one as JList and use the Scala one as just List (i.e. without importing / renaming it). Of course, you are free to apply any renaming that make most sense for you and your team, just wanted to let you know that one I have seen used in other places. Commented Aug 17, 2021 at 12:37

Not the answer you're looking for? Browse other questions tagged or ask your own question.