tags:

views:

136

answers:

1

Hi,

I knew this 'How can I convert a Java Iterable to a Scala Iterable?'

But I am working on java 1.4.2 code with a scala API.

How can I get a scala.Iterable from a java.util.List?

Thank you for your suggestion.

+1  A: 

Everything you need is in scala.lang.JavaConversions

import java.util.{List => JList, ArrayList}
import scala.collection.JavaConversions._

val jul1: JList[String] = new ArrayList[String]; jul1.add("Boo!")

val sb1 = jul1.toBuffer
val ss1 = jul1.toSeq // Same result as toBuffer

This produces a mutable collection in sml1 (a Buffer). If you want an immutable collection (List, e.g.) convert that mutable collection:

val sl1 = jul1.toList

Edit: Hmm... Java 1.4.2? That's pre-generics? (I lose track of such ancient history...) This probably won't work, then... You'll probably need to work with existential types.

Randall Schulz
But I would like some java code...
enguerran
You know in enterprise we have some pre-19700101 code ^^ No elegant method therefore, just loop on java Collection and manual adding in a scala Collection...
enguerran
You said you wanted a `scala.Iterable`. I assumed you were writing Scala code that needed to consume a Java collection.
Randall Schulz
in fact, I have a scala method to call in my java code (from a scala API a colleague has written) which take for parameters a scala.Iterable. But at this step of my source code, I have a java.util.List in version 1.4.2 (that does not implement java.util.Iterable since it does not exist in 1.4.2). And I would like to call the scala method fed by a scala.Iterable with my "not 1.5 Iterable" java.util.List...
enguerran
Actually, I don't think Scala is compatible with Java 1.4.
Randall Schulz
wrong but thank you for the try.
enguerran