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
|
package
com.sohu.tv.m.servlet;
import
org.apache.commons.collections.CollectionUtils;
import
org.apache.commons.lang.ArrayUtils;
import
java.util.*;
/**
* just for test
*
* @author liweihan
* @time 2016-12-12 17:10
*/
public
class
Test {
public
static
void
main(String[] args) {
String[] arrayA =
new
String[] {
"1"
,
"2"
,
"3"
,
"3"
,
"4"
,
"5"
,
"7"
,
"8"
,
"9"
};
String[] arrayB =
new
String[] {
"3"
,
"3"
,
"4"
,
"4"
,
"5"
,
"6"
,
"7"
};
List<String> a = Arrays.asList(arrayA);
List<String> b = Arrays.asList(arrayB);
List<String> c =
new
ArrayList<String>();
//判断为空
if
(CollectionUtils.isEmpty(c)) {
System.out.println(
"c is empty"
);
}
//判断为非空
if
(CollectionUtils.isNotEmpty(a)) {
System.out.println(
"a is not empty!"
);
}
//并集:必须同时包含a,b的集合
Collection<String> union = CollectionUtils.union(a,b);
//交集:二者公共的集合
Collection<String> intersection = CollectionUtils.intersection(a,b);
//a,b非公有的所有数据集合,即去掉a,b重复的数据后
Collection<String> disjunction = CollectionUtils.disjunction(a,b);
//差集:a集合有,b集合没有的数据集合
Collection<String> subtract = CollectionUtils.subtract(a, b);
//排序
Collections.sort((List<String>) union);
Collections.sort((List<String>) intersection);
Collections.sort((List<String>) disjunction);
Collections.sort((List<String>) subtract);
System.out.println(
"A: "
+ ArrayUtils.toString(a.toArray()));
System.out.println(
"B: "
+ ArrayUtils.toString(b.toArray()));
System.out.println(
"--------------------------------------------"
);
System.out.println(
"Union(A, B): "
+ ArrayUtils.toString(union.toArray()));
System.out.println(
"Intersection(A, B): "
+ ArrayUtils.toString(intersection.toArray()));
System.out.println(
"Disjunction(A, B): "
+ ArrayUtils.toString(disjunction.toArray()));
System.out.println(
"Subtract(A, B): "
+ ArrayUtils.toString(subtract.toArray()));
}
}
|
输出结果:
1
2
3
4
5
6
7
8
9
|
c is empty
a is not empty!
A: {
1
,
2
,
3
,
3
,
4
,
5
,
7
,
8
,
9
}
B: {
3
,
3
,
4
,
4
,
5
,
6
,
7
}
--------------------------------------------
Union(A, B): {
1
,
2
,
3
,
3
,
4
,
4
,
5
,
6
,
7
,
8
,
9
}
Intersection(A, B): {
3
,
3
,
4
,
5
,
7
}
Disjunction(A, B): {
1
,
2
,
4
,
6
,
8
,
9
}
Subtract(A, B): {
1
,
2
,
8
,
9
}
|
本文转自韩立伟 51CTO博客,原文链接:http://blog.51cto.com/hanchaohan/1882038
,如需转载请自行联系原作者