集合之间的求交集、并集和差集会如此简单吗?

tech2026-08-15  0

01 引入jar

<dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-collections4</artifactId> <version>4.4</version> </dependency>

02 CollectionUtils

2.1 集合判空操作

List<Integer> emptyList = new ArrayList<Integer>(); List<Integer> nullList = null; List<Integer> hasValueList = new ArrayList<Integer>(); hasValueList.add(5); hasValueList.add(6); //判断集合是否为空 ----- isEmpty 判断数组是否为空(null) System.out.println(CollectionUtils.isEmpty(emptyList)); //true System.out.println(CollectionUtils.isEmpty(nullList)); //true System.out.println(CollectionUtils.isEmpty(hasValueList)); //false //判断集合是否不为空-----isNotEmpty 判断数组不为空 System.out.println(CollectionUtils.isNotEmpty(emptyList)); //false System.out.println(CollectionUtils.isNotEmpty(nullList)); //false System.out.println(CollectionUtils.isNotEmpty(hasValueList)); //true

2.2 集合比较操作

isEqualCollection 判断两个集合的值是否相等isSubCollection 判断一个集合是否是另外一个集合的子集 List<Integer> c1 = new ArrayList<Integer>(); c1.add(2); c1.add(1); List<Integer> c2 = new ArrayList<Integer>(); c2.add(1); c2.add(2); List<Integer> c3 = new ArrayList<Integer>(); c3.add(1); //比较两集合值 System.out.println(CollectionUtils.isEqualCollection(c1,c2)); //true System.out.println(CollectionUtils.isEqualCollection(c1,c3)); //false System.out.println(CollectionUtils.isSubCollection(c3,c1)); // true System.out.println(CollectionUtils.isSubCollection(c1,c3)); // false

2.3 集合运算

subtract 两个集合的差集intersection 两个集合的交集disjunction 交集的补集union 两个集合的并集 List<Integer> op1 = new ArrayList<Integer>(); op1.add(1); op1.add(2); op1.add(3);; List<Integer> op2 = new ArrayList<Integer>(); op2.add(3); op2.add(3); op2.add(4); op2.add(5); //并集 System.out.println(CollectionUtils.union(op1, op2)); //[1, 2, 3, 3, 4, 5] //交集 System.out.println(CollectionUtils.intersection(op1, op2)); //[3] //交集的补集 System.out.println(CollectionUtils.disjunction(op1, op2)); //[1, 2, 3, 4, 5] //差集 System.out.println(CollectionUtils.subtract(op1, op2)); //[1, 2] System.out.println(CollectionUtils.subtract(op1, op2)); //[3, 4, 5]

2.4 数组倒序

// reverseArray 将数组元素倒序 Integer[] list = new Integer[]{1,2,3,4,5}; CollectionUtils.reverseArray(list); for (int i : list){ System.out.println(i); }

03 MapUtils

3.1 将特定的数组放入map中

Map colorMap = MapUtils.putAll(new HashMap(), new String[][] { {"RED", "#FF0000"}, {"GREEN", "#00FF00"}, {"BLUE", "#0000FF"} }); Map colorMap1 = MapUtils.putAll(new HashMap(), new String[] { "RED", "#FF0000", "GREEN", "#00FF00", "BLUE", "#0000FF" }); Map colorMap3 = MapUtils.putAll(new HashMap(), new Map.Entry[] { new DefaultMapEntry("RED", "#FF0000"), new DefaultMapEntry("GREEN", "#00FF00"), new DefaultMapEntry("BLUE", "#0000FF") });

3.2 map的判空操作

Map<String, String> nullMap = null; Map<String, String> emptyMap = new HashMap<>(); Map<String, String> hasValueMap = new HashMap<>(); hasValueMap.put("1","1"); // 判断map为空(empty,null) System.out.println(MapUtils.isEmpty(nullMap)); //true System.out.println(MapUtils.isEmpty(emptyMap)); //true System.out.println(MapUtils.isEmpty(hasValueMap)); //false // 判断map不为空 System.out.println(MapUtils.isNotEmpty(nullMap)); //false System.out.println(MapUtils.isNotEmpty(emptyMap)); //false System.out.println(MapUtils.isNotEmpty(hasValueMap)); //true

04 参考文档

官方文档:http://commons.apache.org/proper/commons-collections/userguide.html

 

最新回复(0)