目录
  1. 1. CollectUtils
    1. 1.1. pom依赖
    2. 1.2. 常用API
    3. 1.3. 非对象集合交、并、差处理
      1. 1.3.1. 1. CollectionUtils工具类
      2. 1.3.2. 2.List自带方法
      3. 1.3.3. 3、JDK1.8 stream 新特性
    4. 1.4. 对象集合交、并、差处理
      1. 1.4.1. 1、Person对象
      2. 1.4.2. 2、测试
  2. 2. MapUtils
集合工具类

CollectUtils

  通过apache-commons包中的org.apache.commons.collections.CollectionUtils集合操作工具类对集合间进行合并union、交叉intersection、分离disjunction、减去subtract、任意包含containsAny、判断是否为子集isSubCollection、颠倒序列reverseArray及判断是否填满isFull等操作
  CollectionsUtils在真实项目中,可以使你的代码更加简洁和安全

pom依赖

1
2
3
4
5
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
<version>4.4</version>
</dependency>

常用API

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/**
* 除非元素为null,否则向集合添加元素
*/
CollectionUtils.addIgnoreNull(personList,null);

/**
* 空安全检查指定的集合是否为空
*/
CollectionUtils.isEmpty(Collection<?> coll)

/**
* 空安全检查指定的集合是否为空。
*/
CollectionUtils.isNotEmpty(Collection<?> coll)

/**
* 反转给定数组的顺序。
*/
CollectionUtils.reverseArray(Object[] array);

/**
* 获取集合可以包含的最大元素数量
*/
int maxSize(final Collection<? extends Object> coll)

/**
* 集合并集
*/
CollectionUtils.union(final Iterable<? extends O> a, final Iterable<? extends O> b)

/**
* 集合交集
*/
CollectionUtils.intersection(final Iterable<? extends O> a, final Iterable<? extends O> b)

/**
* 交集的补集(析取)
*/
CollectionUtils.disjunction(final Iterable<? extends O> a, final Iterable<? extends O> b)

/**
* 两集合的差集
*/
CollectionUtils.subtract(final Iterable<? extends O> a, final Iterable<? extends O> b)

/**
* coll1是否全部包含coll2
*/
boolean containsAll(final Collection<?> coll1, final Collection<?> coll2)

/**
* 两个集合中至少有一个元素都含有
*/
boolean containsAny(final Collection<?> coll1, final T... coll2)

/**
* 将Collection的每个元素作为key, 元素出现的次数作为value, 整理到Map输出
*/
Map<O, Integer> getCardinalityMap(final Iterable<? extends O> coll)

/**
* 如果a是b的子集合,则返回true
*/
boolean isSubCollection(final Collection<?> a, final Collection<?> b)

/**
* 如果a是b的子集合且a的size < b的size,则返回true
*/
boolean isProperSubCollection(final Collection<?> a, final Collection<?> b)

/**
* 判断a和b两集合是否完全相同
*/
boolean isEqualCollection(final Collection<?> a, final Collection<?> b)

/**
* 通过对每个元素应用Predicate来过滤集合
*/
boolean filter(final Iterable<T> collection, final Predicate<? super T> predicate)

/**
* 通过对每个元素应用一个Transformer来变换集合
*/
void transform(final Collection<C> collection, final Transformer<? super C, ? extends C> transformer)

/**
* 从输入集合中选择所有与给定Predicate匹配的元素到输出集合中
*/
Collection<O> select(final Iterable<? extends O> inputCollection, final Predicate<? super O> predicate)

/**
* 从输入集合中选择所有与给定Predicate不匹配的元素到输出集合中
*/
Collection<O> selectRejected(final Iterable<? extends O> inputCollection, final Predicate<? super O> predicate)

/**
* 返回一个新Collection,其中包含由给定Transformer转换的输入集合的所有元素
*/
Collection<O> collect(final Iterable<I> inputCollection, final Transformer<? super I, ? extends O> transformer)

/**
* 将两个排序的集合a和b合并到一个单独的排序列表中,以便保留元素的自然顺序
*/
List<O> collate(final Iterable<? extends O> a,final Iterable<? extends O> b)

非对象集合交、并、差处理

对于集合取交集、并集的处理其实有很多种方式,这里就介绍3种

  • 第一种 是CollectionUtils工具类
  • 第二种 是List自带方法
  • 第三种 是JDK1.8 stream 新特性

1. CollectionUtils工具类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public static void main(String[] args) {
String[] arrayA = new String[] { "1", "2", "3", "4"};
String[] arrayB = new String[] { "3", "4", "5", "6" };
List<String> listA = Arrays.asList(arrayA);
List<String> listB = Arrays.asList(arrayB);

//1、并集 union
System.out.println(CollectionUtils.union(listA, listB));
//输出: [1, 2, 3, 4, 5, 6]

//2、交集 intersection
System.out.println(CollectionUtils.intersection(listA, listB));
//输出:[3, 4]

//3、交集的补集(析取)disjunction
System.out.println(CollectionUtils.disjunction(listA, listB));
//输出:[1, 2, 5, 6]

//4、差集(扣除)
System.out.println(CollectionUtils.subtract(listA, listB));
//输出:[1, 2]
}

2.List自带方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
public static void main(String[] args) {
String[] arrayA = new String[] { "1", "2", "3", "4"};
String[] arrayB = new String[] { "3", "4", "5", "6" };
List<String> listA = Arrays.asList(arrayA);
List<String> listB = Arrays.asList(arrayB);

//1、交集
List<String> jiaoList = new ArrayList<>(listA);
jiaoList.retainAll(listB);
System.out.println(jiaoList);
//输出:[3, 4]

//2、差集
List<String> chaList = new ArrayList<>(listA);
chaList.removeAll(listB);
System.out.println(chaList);
//输出:[1, 2]

//3、并集 (先做差集再做添加所有)
List<String> bingList = new ArrayList<>(listA);
bingList.removeAll(listB); // bingList为 [1, 2]
bingList.addAll(listB); //添加[3,4,5,6]
System.out.println(bingList);
//输出:[1, 2, 3, 4, 5, 6]
}

intersection和retainAll的差别:
  要注意的是它们的返回类型是不一样的,intersection返回的是一个新的List集合,而retainAll返回是Bollean类型那就说明retainAll方法是对原有集合进行处理再返回原有集合,会改变原有集合中的内容

个人观点:

  1. 从性能角度来考虑的话,List自带会高点,因为它不用再创建新的集合
  2. 需要注意的是:因为retainAll因为会改变原有集合,所以该集合需要多次使用就不适合用retainAll

注意:Arrays.asList将数组转集合不能进行add和remove操作
原因:调用Arrays.asList()生产的List的add、remove方法时报异常,这是由于Arrays.asList() 返回的市Arrays的内部类ArrayList, 而不是java.util.ArrayList.Arrays的内部类ArrayList和java.util.ArrayList都是继承AbstractList,remove、add等方法AbstractList中是默认throw UnsupportedOperationException而且不作任何操作.java.util.ArrayList重新了这些方法而Arrays的内部类ArrayList没有重新,所以会抛出异常
正确做法:

1
2
3
4
String[] array = {"1","2","3","4","5"};
List<String> list = Arrays.asList(array);
List arrList = new ArrayList(list);
arrList.add("6");

3、JDK1.8 stream 新特性

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
public static void main(String[] args) {
String[] arrayA = new String[] { "1", "2", "3", "4"};
String[] arrayB = new String[] { "3", "4", "5", "6" };
List<String> listA = Arrays.asList(arrayA);
List<String> listB = Arrays.asList(arrayB);

// 交集
List<String> intersection = listA.stream().filter(item -> listB.contains(item)).collect(toList());
System.out.println(intersection);
//输出:[3, 4]

// 差集 (list1 - list2)
List<String> reduceList = listA.stream().filter(item -> !listB.contains(item)).collect(toList());
System.out.println(reduceList);
//输出:[1, 2]

// 并集 (新建集合:1、是因为不影响原始集合。2、Arrays.asList不能add和remove操作。
List<String> listAll = listA.parallelStream().collect(toList());
List<String> listAll2 = listB.parallelStream().collect(toList());
listAll.addAll(listAll2);
System.out.println(listAll);
//输出:[1, 2, 3, 4, 3, 4, 5, 6]

// 去重并集
List<String> list =new ArrayList<>(listA);
list.addAll(listB);
List<String> listAllDistinct = list.stream().distinct().collect(toList());
System.out.println(listAllDistinct);
//输出:[1, 2, 3, 4, 5, 6]
}

对象集合交、并、差处理

1、Person对象

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
public class Person {
private String name;
private Integer age;
public Person(String name, Integer age) {
this.name = name;
this.age = age;
}
/**
* 为什么重写equals方法一定要重写hashCode方法下面也会讲
*/
@Override
public int hashCode() {
String result = name + age;
return result.hashCode();
}
/**
* 重写 equals 方法 根据name和age都相同那么对象就默认相同
*/
@Override
public boolean equals(Object obj) {
Person u = (Person) obj;
return this.getName().equals(u.getName()) && (this.age.equals(u.getAge()));
}
/**
* 重写 toString 方法
*/
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}

2、测试

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
public static void main(String[] args) {
List<Person> personList = Lists.newArrayList();
Person person1 = new Person("小小",3);
Person person2 = new Person("中中",4);
personList.add(person1);
personList.add(person2);

List<Person> person1List = Lists.newArrayList();
Person person3 = new Person("中中",4);
Person person4 = new Person("大大",5);
person1List.add(person3);
person1List.add(person4);
/**
* 1、差集
*/
System.out.println(CollectionUtils.subtract(personList, person1List));
//输出:[Person{name='小小', age=3}]

/**
* 2、并集
*/
System.out.println(CollectionUtils.union(personList, person1List));
//输出:[Person{name='小小', age=3}, Person{name='中中', age=4}, Person{name='大大', age=5}]

/**
* 3、交集
*/
System.out.println(CollectionUtils.intersection(personList, person1List));
//输出:[Person{name='中中', age=4}]

/**
* 4、交集的补集(析取)
*/
System.out.println(CollectionUtils.disjunction(personList, person1List));
//输出:[Person{name='小小', age=3}, Person{name='大大', age=5}]
}

MapUtils

  对获取默认值及其类型自动处理转换(getObject、getString、getBoolean、getNumber、getByte、getShort、getInteger、getLong、getFloat、getDouble、getMap)及toProperties转换配置类型、空判断isEmpty\isNotEmpty等通用操作处理

文章作者: Eric Liang
文章链接: https://ericql.github.io/2019/11/12/01-Java%E5%9F%BA%E7%A1%80%E7%AF%87/03-%E9%9B%86%E5%90%88%E7%AF%87/%E9%9B%86%E5%90%88%E5%B7%A5%E5%85%B7%E7%B1%BB/
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 Eric Liang
打赏
  • 微信
  • 支付宝