Best scala questions in April 2012

comparing Scala lists with Java lists

13 votes

How can I compare a Scala list with a Java list?

scala> List(1, 2, 3, 4, 5)
res0: List[Int] = List(1, 2, 3, 4, 5)

scala> java.util.Arrays.asList(1, 2, 3, 4, 5)
res1: java.util.List[Int] = [1, 2, 3, 4, 5]

scala> res0 == res1
res2: Boolean = false

Is there some static helper method for comparison that accepts both Scala lists and Java lists? Or is there a kind of "lazy wrapper" over both sorts of lists which I can then directly compare via ==?

... or use sameElements.

scala> import collection.JavaConversions._
import collection.JavaConversions._

scala> res0.sameElements(res1)            
res3: Boolean = true