Convert Java Map to Scala Map

Edit: the recommended way is now to use JavaConverters and the .asScala method: import scala.collection.JavaConverters._ val myScalaMap = myJavaMap.asScala.mapValues(_.asScala.toSet) This has the advantage of not using magical implicit conversions but explicit calls to .asScala, while staying clean and consise. The original answer with JavaConversions: You can use scala.collection.JavaConversions to implicitly convert between Java and Scala: … Read more

How to convert a scala.List to a java.util.List?

Not sure why this hasn’t been mentioned before but I think the most intuitive way is to invoke the asJava decorator method of JavaConverters directly on the Scala list: scala> val scalaList = List(1,2,3) scalaList: List[Int] = List(1, 2, 3) scala> import scala.collection.JavaConverters._ import scala.collection.JavaConverters._ scala> scalaList.asJava res11: java.util.List[Int] = [1, 2, 3]

Using Scala traits with implemented methods in Java

Answer From Java perspective Trait.scala is compiled into Trait interface. Hence implementing Trait in Java is interpreted as implementing an interface – which makes your error messages obvious. Short answer: you can’t take advantage of trait implementations in Java, because this would enable multiple inheritance in Java (!) How is it implemented in Scala? Long … Read more

What is the difference between JavaConverters and JavaConversions in Scala?

EDIT: Java Conversions got @deprecated in Scala 2.13.0. Use scala.jdk.CollectionConverters instead. JavaConversions provide a series of implicit methods that convert between a Java collection and the closest corresponding Scala collection, and vice versa. This is done by creating wrappers that implement either the Scala interface and forward the calls to the underlying Java collection, or … Read more