import java.util.Set;
import java.util.HashSet;
import java.util.Collections;
public class DisjointSetsExample {
public static void main(String[] args) {
// 创建两个集合
Set<Integer> set1 = new HashSet<>();
set1.add(1);
set1.add(2);
set1.add(3);
Set<Integer> set2 = new HashSet<>();
set2.add(4);
set2.add(5);
set2.add(6);
// 检查两个集合是否不相交
boolean areDisjoint = Collections.disjoint(set1, set2);
// 输出结果
System.out.println("Set1: " + set1);
System.out.println("Set2: " + set2);
System.out.println("Are the sets disjoint? " + areDisjoint);
}
}
Set<Integer> set1 和 Set<Integer> set2 分别创建了两个集合,并添加了一些元素。Collections.disjoint(set1, set2) 方法用于检查两个集合是否不相交(即两个集合没有共同的元素)。如果两个集合没有任何共同元素,则返回 true,否则返回 false。上一篇:java neo4j
下一篇:java截取
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站