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
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}
You can use Fully Qualified Name (FQN)
- Java
List
isjava.utils.List
- Scala
List
isscala.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
.
-
@itisha minor note that, in general, the convention is to just import the Java one as
JList
and use the Scala one as justList
(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