2

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.

2
  • Is that running on a 1.5 or 1.6 JVM, with 1.4 source code, or is it full 1.4? Because Scala 2.8, afaik, does not support Java 1.4. Commented Sep 23, 2010 at 14:21
  • It is running with scala 2.7.4 on java 1.4 with a JVM 1.4 Commented Sep 23, 2010 at 14:24

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

4 Comments

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...
You said you wanted a scala.Iterable. I assumed you were writing Scala code that needed to consume a Java collection.
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...
Actually, I don't think Scala is compatible with Java 1.4.

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.