比较两个集合中有没有相同的元素
java.util.Collections.disjoint(Collection<?> c1, Collection<?> c2)
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
/**
* @ClassName CollectionsOfDisjoint
* @Description 比较两个集合中是否有相同的元素;当两个集合中没有相同元素时返回true,当有相同元素时返回false。
* @Version 1.0
**/
public class CollectionsOfDisjoint {
public static void main(String[] args) {
Set<String> set1 = new HashSet<>();
set1.add("111");
set1.add("222");
set1.add("333");
Set<String> set2 = new HashSet<>();
set2.add("444");
set2.add("555");
set2.add("666");
Set<String> set3 = new HashSet<>();
set3.add("444");
set3.add("777");
set3.add("888");
boolean a = Collections.disjoint(set1, set2);
System.out.println("set1 与 set2 无相同元素时,Collections.disjoint结果:" + a);
boolean b = Collections.disjoint(set2, set3);
System.out.println("set2 与 set3 有相同元素时,Collections.disjoint结果:" + b);
}
}
以下是结果:
![](https://img-blog.csdnimg.cn/img_convert/f25da7cbd3157487ea40f0b522a8825c.png)