public static void main(String[] args) {
List<Integer> in1 = Arrays.asList(1, 2, 3, 4, 4, 5);
List<Integer> in2 = Arrays.asList(4, 5, 6);
//集合不为空判断
boolean notEmpty = CollectionUtils.isNotEmpty(in1);
System.out.println(notEmpty);//true
//集合为空判断,与isEmpty()区别于多了null值判断,很实用
boolean empty = CollectionUtils.isEmpty(in2);
System.out.println(empty);
//取并集
Collection<Integer> union = CollectionUtils.union(in1, in2);
System.out.println(union);//[1, 2, 3, 4, 4, 5, 6]
//取交集
Collection<Integer> intersection = CollectionUtils.intersection(in1, in2);
System.out.println(intersection);//[4, 5]
//取交集的补集
Collection<Integer> disjunction = CollectionUtils.disjunction(in1, in2);
System.out.println(disjunction);//[1, 2, 3, 4, 6]
//取差in1与in2的差
Collection<Integer> subtract = CollectionUtils.subtract(in1, in2);
System.out.println(subtract);
}
Apache-commons:collections4.CollectionUtils工具类
最新推荐文章于 2025-06-04 13:57:43 发布