tou*_*eqt 8 java collections scala apache-commons
标题说明一切,找出集合是否包含其他集合的任何元素的最佳实践是什么?
在java中我会像这样执行它
CollectionUtils.containsAny(a, b)
Run Code Online (Sandbox Code Playgroud)
使用常见的apache collection utils,其中变量a/b是集合.
如何在scala中实现此行为?或者是否有像上面的CollectionUtils这样的库?
我不想使用common-apache库,因为我必须将scala集合转换为java集合.
Pet*_*ens 20
您可以使用组合exists(p: T => Boolean):Boolean和contains(elem: A1):Boolean:
val a = List(1,2,3,4,5,6,7)
val b = List(11,22,33,44,55,6)
a.exists(b.contains) // true
Run Code Online (Sandbox Code Playgroud)
jav*_*dba 11
相交
val a = Seq(1,2,3) ; val b = Seq(2,4,5)
a.intersect(b)
res0: Seq[Int] = List(2)
// to include the test:
a.intersect(b).nonEmpty // credit @Lukasz
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7495 次 |
| 最近记录: |