1.交集
java
复制代码
List<Integer> list1=Arrays.asList(1,2,3);
List<Integer> list2=Arrays.asList(3,4,5);
//list1 ∩ list2
System.out.println(CollectionUtils.intersection(list2, list1)); //[3]
2.差集
java
复制代码
List<Integer> list1=Arrays.asList(1,2,3);
List<Integer> list2=Arrays.asList(3,4,5);
//list1-list2(list1中删除list2共有数据)
System.out.println(CollectionUtils.subtract(list1, list2));//[1,2]
//list2-list1差集(list2中删除list1共有数据)
System.out.println(CollectionUtils.subtract(list2, list1)); //[4,5]
3.并集
java
复制代码
List<Integer> list1=Arrays.asList(1,2,3);
List<Integer> list2=Arrays.asList(3,4,5);
//list1 ∪ list2
System.out.println(CollectionUtils.union(list2, list1)); //[1,2,3,4,5]
4.交集的补集
java
复制代码
List<Integer> list1=Arrays.asList(1,2,3);
List<Integer> list2=Arrays.asList(3,4,5);
// (list1 + list2) - (list1 ∪ list2)
System.out.println(CollectionUtils.disjunction(list2, list1)); //[]
5.集合元素是否相等
java
复制代码
List<Integer> list1=Arrays.asList(1,2,3);
List<Integer> list2=Arrays.asList(3,4,5);
// list1 == list2
System.out.println(CollectionUtils.isEqualCollection(list2, list1)); //false
List<Integer> list3=Arrays.asList(1,2,3);
List<Integer> list4=Arrays.asList(3,2,1);
// list3 == list4
System.out.println(CollectionUtils.isEqualCollection(list3, list4)); //true
6.集合判空
java
复制代码
List<Integer> list1=Arrays.asList(1,2,3);
System.out.println(CollectionUtils.isEmpty(list1)); //false
System.out.println(CollectionUtils.isNotEmpty(list1)); //true
7.是否包含任一
java
复制代码
List<Integer> list1 = Arrays.asList(1,2,3);
List<Integer> list2 = Arrays.asList(3,2,1);
List<Integer> list3 = Arrays.asList(3);
List<List<Integer>> all = Lists.newArrayList(list1,list2,list3);
System.out.println(CollectionUtils.containsAny(all,list1,list2));//true
System.out.println(CollectionUtils.containsAny(list1,4,5));//false
System.out.println(CollectionUtils.containsAny(list1,2,4));//true
System.out.println(CollectionUtils.containsAny(list2,list1));//true
8.是否包含全部
java
复制代码
List<Integer> list1=Arrays.asList(1,2,3);
List<Integer> list2=Arrays.asList(3,4,5);
// list1.sort() == list2.sort()
System.out.println(CollectionUtils.containsAll(list1, list2)); //false
List<Integer> list3=Arrays.asList(1,2,3);
List<Integer> list4=Arrays.asList(3,2,1);
// list3.sort() == list4.sort()
System.out.println(CollectionUtils.containsAll(list3, list4)); //true
9.是否为子集
java
复制代码
List<Integer> list1=Arrays.asList(1,2,3);
List<Integer> list2=Arrays.asList(3,4,5);
// list1.sort().contains(list2.sort())
System.out.println(CollectionUtils.isSubCollection(list1, list2)); //false
List<Integer> list3=Arrays.asList(1,2,3);
List<Integer> list4=Arrays.asList(3,2,1);
// list3.sort().contains(list4.sort())
System.out.println(CollectionUtils.isSubCollection(list3, list4)); //true
System.out.println(CollectionUtils.containsAll(list3, list4));//true