Reputation: 445
I know that there are two extremely useful objects in scala.collection package, which will help us to achieve this goal:
but I have some troubles to apply them in my case because my data structure is a little bit more complex than the other ones that I saw in many examples.
I'm in scala code and I want that my scala function return a Java collection. So I wanto to convert Scala Seq[(Int, Seq[String])] to Java collection List[(int, List[String])].
How can I do this?
Upvotes: 3
Views: 1295
Reputation: 16076
Use map to at first map Seq[(Int, Seq[String])]
to Seq[(Int, List[String])]
and then use again the same function to main collection
import scala.collection.JavaConversions
val seq : Seq[(Int, Seq[String])] = some code
val seqOfLists = seq.map(s => (s._1, JavaConversions.seqAsJavaList(s._2)))
val listOfLists = JavaConversions.seqAsJavaList(seqOfLists)
Or newer API:
import scala.collection.JavaConverters._
val seq : Seq[(Int, Seq[String])] = some code
val seqOfLists = seq.map(s => (s._1, s._2.asJava))
val listOfLists = seqOfLists.asJava
Upvotes: 7
Reputation: 19517
JavaConversions
is deprecated since Scala 2.12.0. Use JavaConverters
instead:
import scala.collection.JavaConverters._
val seq: Seq[(Int, Seq[String])] = ???
val javaList = seq.map(x => (x._1, x._2.asJava)).asJava
// java.util.List[(Int, java.util.List[String])]
Upvotes: 3