类似于'包含任何'的Java集? [英] Something like 'contains any' for Java set?
本文介绍了类似于'包含任何'的Java集?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两套相同类型的套装,A和B.
I have two sets, A and B, of the same type.
我必须找到A是否包含套装B中的任何元素。
I have to find if A contains any element from the set B.
如果不迭代集合,最好的方法是什么?
Set库有包含(对象)
和 containsAll(collection)
,但不是 containsAny(collection)
。
What would be the best way to do that without iterating over the sets?
The Set library has contains(object)
and containsAll(collection)
, but not containsAny(collection)
.
推荐答案
不会Collections.disjoint (A,B)
有效吗?从文档中:
Wouldn't Collections.disjoint(A, B)
work? From the documentation:
如果两个指定的集合中没有元素,则返回
true
common。
因此,如果集合包含任何常见的,则该方法返回 false
元素。
Thus, the method returns false
if the collections contains any common elements.
这篇关于类似于'包含任何'的Java集?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文