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

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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值