3

I want to convert an Scala array to Java list.

val legends = Array("0-500", "500-1000", "1000-2000", "2000-3000", "3000+")

to Java List.

1
  • Scala.Collections.JavaComvertors Commented Dec 25, 2018 at 3:49

1 Answer 1

14

Same way you would convert a java array to a list (scala arrays are the same as java, so no surprise there):

java.util.Arrays.asList(legends:_*)

:_* is called a splat. It is needed to tell the compiler that you want to pass elements of the array as separate varags parameters, not the whole array as one parameter.

Or you can do it explicitly:

import scala.collection.JavaConverters._
val javaList = legends.toList.asJava

Or implicitly:

import scala.collection.JavaConversions._
val javaList: java.util.List[String] = legends.toList
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.