<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
<version>4.4</version>
</dependency>
判断两集合是否相等
List<String> list1 = Arrays.asList("1", "2");
List<String> list2 = Arrays.asList("1", "3");
boolean flag = ListUtils.isEqualList(list1, list2);
System.out.println(flag);
//输出:false
集合合并(去重)
List<String> list1 = Arrays.asList("1", "2");
List<String> list2 = Arrays.asList("1", "3");
List<String> list3 = ListUtils.sum(list1, list2);
System.out.println(list3);
//输出:[2, 1, 3]
取并集
List<String> list1 = Arrays.asList("1", "2");
List<String> list2 = Arrays.asList("1", "3");
List<String> list3 = ListUtils.union(list1, list2);
//输出:[1, 2, 1, 3]
取交集
List<String> list1 = Arrays.asList("1", "2");
List<String> list2 = Arrays.asList("1", "3");
List<String> list3 = ListUtils.intersection(list1, list2);
//输出:[1]
List<String> list4 = ListUtils.retainAll(list1, list2);
//输出:[1]
取差集(集合1去除集合2中的所有元素)
List<String> list1 = Arrays.asList("1", "2");
List<String> list2 = Arrays.asList("1", "3");
List<String> list3 = ListUtils.subtract(list1, list2);
//输出:[2]
List<String> list4 = ListUtils.removeAll(list1, list2);
//输出:[2]
生成固定类型列表
List<Object> list1 = Arrays.asList("1", "2");
List<Object> list3 = ListUtils.typedList(list1, String.class);
//输出:[1, 2]
list3.add(1);
//输出:java.lang.IllegalArgumentException:
//Cannot add Object '1' - Predicate rejected it
生成固定大小列表
List<String> list1 = Arrays.asList("1", "2");
List<String> list2 = ListUtils.fixedSizeList(list1);
//输出:[1, 2]
list1.add("3");
//输出:java.lang.UnsupportedOperationException
给集合加锁
List<String> list1 = Arrays.asList("1", "2");
List<String> list2 = ListUtils.synchronizedList(list1);
//输出:[1, 2]
生成不可修改集合(不能增减元素,只能查询)
List<String> list1 = Arrays.asList("1", "2");
List<String> list2 = ListUtils.unmodifiableList(list1);
//输出:[1, 2]
list1.add("3");
//输出:java.lang.UnsupportedOperationException
标签:Arrays,list1,List,ListUtils,list2,集合,apache,asList From: https://www.cnblogs.com/xfeiyun/p/17869344.html