[英]comparing Scala lists with Java lists
How can I compare a Scala list with a Java list? 如何将Scala列表与Java列表进行比较?
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? 是否有一些用于比较的静态助手方法接受Scala列表和Java列表? Or is there a kind of "lazy wrapper" over both sorts of lists which I can then directly compare via ==
? 或者在两种列表中都有一种“懒惰包装”,然后我可以直接通过==
进行比较?
... or use sameElements
. ...或使用sameElements
。
scala> import collection.JavaConversions._
import collection.JavaConversions._
scala> res0.sameElements(res1)
res3: Boolean = true
You can use JavaConverters
for this: 您可以使用JavaConverters
:
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. 需要注意的是asScala
只返回原始视图List
,请参阅的文档asScalaBufferConverter
在JavaConverters文档。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.