Convert java.util.Set to java.util.List in Scala

In a project that is a combination of Scala and Java, I need to convert the Java set into a Java list, and in the Scala part of the code.

What are some effective ways to do this? I could use JavaConverters to convert a Java Set -> Scala Set -> Scala List -> Java List. Are there other options that will be more effective?

thank

+4
source share
2 answers

Java collection classes provide a constructor that accepts Collection, so why not just use this?

def js2jl[A](s: java.util.Set[A]): java.util.List[A] = new java.util.ArrayList(s)

Nothing Scala-specific outside the syntax, but it is not bad in this case.

+4

, Java? :

val mySet : java.util.Set[Integer] = new java.util.HashSet()
mySet.add(5)
val myList : java.util.List[Integer] = new java.util.ArrayList(mySet)
println(myList)

, ?

+2

All Articles