Convert a Scala Buffer to Java ArrayList
Asked Answered
P

3

8

In my Scala function, i'm traversing a Java ArrayCollection, extracting specific elements which should form a new collection. At the end, it has to be a Java-ArrayList again, because i'm interacting with a Java Framework. My Code:

// to make scala-style iterating over arraylist possible
import scala.collection.JavaConversions._

// ArrayList contains elements of this type:
class Subscription(val subscriber:User, val sender:User)

// I'm getting this list from Java:
val jArrayList = new ArrayList[Subscription]
// Buffer:scala.collection.mutable.Buffer[User]
val buffer = for (val subscription <- jArrayList ) yield subscription.sender

How can i convert the Buffer to an ArrayList[User]? Or shouldn't i use yield here?

Paring answered 20/7, 2010 at 11:52 Comment(1)
This is a super old thread, but for anyone looking to do the same -- the accepted answer here now deprecated. Use scala.jdk.CollectionConverters insteadDora
D
8

You should be able to convert it back by specifying what type you'd like buffer to be (JavaConversions should be brought into play automatically when the type you're trying to get and the one you have are incompatible):

val buffer: java.util.List[User] =
  for (val subscription <- jArrayList ) yield subscription.sender

Alternatively you can call the conversion from JavaConversions explicitly if you want to make it clear what you're doing:

val buffer = asList( for ( ... ) ) // buffer should have type "java.util.List[User]"

Neither of these actually produce an ArrayList; rather, they create a generic List, but it's generally bad practice to specify collection types directly. If you must have an ArrayList, pass your List to the constructor of ArrayList which takes a Collection:

new ArrayList( buffer )
Dispread answered 20/7, 2010 at 12:18 Comment(0)
K
2

You could pass the buffer in the ArrayList constructor to create a new java ArrayList:

var newArray = new ArrayList[Int] (buffer)
Karyoplasm answered 20/7, 2010 at 11:58 Comment(1)
This would probably work due to the fact that it will be converted implicitly to a java.util.List. Good stuff, although sometimes you really don't need an ArrayList rather than just a List.Dispread
E
1

When one uses for comprehensions, the underlying code uses map, flatMap, foreach, filter or withFilter -- see related questions. In this particular example, that code would be equivalent to jArrayList.map(_.sender), and map produces new collections (I'm ignoring the implicit conversion here to keep it simple).

What happens here that is perhaps unintuitive, and perhaps could be improved on, is that ArrayList does not implement map. The implicit conversion gives you a Buffer instead, and a map on a Buffer returns a Buffer.

Granted, the Buffer on JavaConversions preserves the underlying collection, the ArrayList, as the backing store. The newly created Buffer, on the other hand, won't be based on that, but on one of Scala's own collections.

You could always do this, though:

val buffer = (for (val subscription <- jArrayList ) yield subscription.sender).asList
Exclosure answered 20/7, 2010 at 12:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.