piyush aggarwal
piyush aggarwal

Reputation: 87

Convert Scala Seq to Java List

I am working in Java. I am calling a scala function which returns a Seq[String]. To convert it to java's List, I tried using scala.collection.JavaConverters's asJava. But this does not seem to work.

Answers on similar questions have suggested either using JavaConversions or WrapAsJava, both of which are deprecated.

Similar Question - Converting Scala seq<string> to Java List<string>

//someScalaFunc returns a Seq[String]
List<String> listA = someScalaFunc(); 

Upvotes: 5

Views: 15029

Answers (3)

Learner
Learner

Reputation: 1190

import scala.collection.JavaConverters._

val scalaList = List("A", "B", "c")

scalaList.asJava

Upvotes: 0

chengpohi
chengpohi

Reputation: 14227

Since JavaConversions is deprecated, You can use JavaConverters for this:

List<String> listA = scala.collection.JavaConverters.seqAsJavaList(someScalaFunc())

Upvotes: 12

Sohum Sachdev
Sohum Sachdev

Reputation: 1397

You can use a Java to Scala implicit converter by importing the following:

import scala.collection.JavaConversions._

Upvotes: 2

Related Questions