Ayush
Ayush

Reputation: 45

How to convert Scala Array to Java List ?

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.

Upvotes: 3

Views: 6411

Answers (1)

Dima
Dima

Reputation: 40500

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

Upvotes: 14

Related Questions