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
You can use JavaConverters
for this:
scala> import collection.JavaConverters._
import collection.JavaConverters._
scala> res0 == res1.asScala
res2: Boolean = true
Note that the asScala
only returns a view on the original List
, see documentation of asScalaBufferConverter
in JavaConverters documentation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With