iterating over Scala collections in Java

barroco picture barroco · Sep 6, 2010 · Viewed 7.2k times · Source

How I can iterate over Scala collections in Java?

Answer

Synesso picture Synesso · Sep 6, 2010

Some example Scala

class AThing {
  @scala.reflect.BeanProperty val aList = List(1,2,3,4,99)
}

A Java client

public class UseAThing {
  public static void main(String a[]) {
    AThing thing = new AThing();
    scala.collection.Iterator iter = thing.getAList().iterator();
    while (iter.hasNext()) {
      System.out.println(iter.next());
    }
  }
}

Output

jem@Respect:~/c/user/jem$ java -cp /opt/scala/lib/scala-library.jar:. UseAThing
1
2
3
4
99

Does that help?