java集合的交集

交集 Intersection 英 [ˌɪntəˈsekʃn]
并集 Union 英 [ˈjuːniən]
补集 Complement 英 [ˈkɒmplɪment]

交集

1、java.util.List#retainAll

侵入式取交集,会改变原集合

List<Integer> list1 = new ArrayList(Arrays.asList(1, 2, 3, 4, 5));
List<Integer> list2 = new ArrayList(Arrays.asList(1, 2, 3, 6, 7, 8, 9));
System.out.println(list1);
System.out.println(list2);
list1.retainAll(list2);
System.out.println(list1);
System.out.println(list2);

[1, 2, 3, 4, 5]
[1, 2, 3, 6, 7, 8, 9]
交集结果是: [1, 2, 3] 
[1, 2, 3, 6, 7, 8, 9]

规避侵入式取交集

List<Integer> list1 = new ArrayList(Arrays.asList(1, 2, 3, 4, 5));
List<Integer> list2 = new ArrayList(Arrays.asList(1, 2, 3, 6, 7, 8, 9));
System.out.println(list1);
System.out.println(list2);

List<Integer> list3 = new ArrayList(list1);
list3.retainAll(list2);
System.out.println(list1);
System.out.println(list2);
System.out.println(list3);

[1, 2, 3, 4, 5]
[1, 2, 3, 6, 7, 8, 9]
[1, 2, 3, 4, 5]
[1, 2, 3, 6, 7, 8, 9]
交集结果是: [1, 2, 3]

这种居然不算有交→→→→→→→→→→这里他妈的有大坑啊

List<Integer> list1 = new ArrayList(Arrays.asList(1, 2, 3, 6, 7, 8, 9));
List<Integer> list2 = new ArrayList(Arrays.asList(1, 2, 3, 6, 7, 8, 9));
System.out.println(list1);
System.out.println(list2);
list1.retainAll(list2);
System.out.println(list1);
System.out.println(list2);
[1, 2, 3, 6, 7, 8, 9]
[1, 2, 3, 6, 7, 8, 9]
交集结果是:  [1, 2, 3, 6, 7, 8, 9]
[1, 2, 3, 6, 7, 8, 9]

判断是否有交集

List<Integer> list1 = new ArrayList(Arrays.asList(1, 2, 3, 4, 5));
List<Integer> list2 = new ArrayList(Arrays.asList(1, 2, 3, 6, 7, 8, 9));
List<Integer> list3 = new ArrayList(list1);
boolean existIntersection  = list3.retainAll(list2);
true
boolean existIntersection2 = new ArrayList(list1).retainAll(list2);
true

// 这种居然不算有交集→→→→→→→→→→→→→→→→→→→→→这里他妈的有大坑啊
List<Integer> list1 = new ArrayList(Arrays.asList(1, 2, 3, 6, 7, 8, 9));
List<Integer> list2 = new ArrayList(Arrays.asList(1, 2, 3, 6, 7, 8, 9));
boolean existIntersection2 = new ArrayList(list1).retainAll(list2);
false

List<Integer> list1 = new ArrayList();
List<Integer> list2 = new ArrayList();
boolean existIntersection2 = new ArrayList(list1).retainAll(list2);
false

List<Integer> list1 = new ArrayList();
List<Integer> list2 = null;
boolean existIntersection2 = new ArrayList(list1).retainAll(list2);
NullPointerException

List<Integer> list1 = null;
List<Integer> list2 = new ArrayList();
boolean existIntersection2 = new ArrayList(list1).retainAll(list2);
NullPointerException

2、org.apache.commons.collections.CollectionUtils#intersection

List<Integer> list1 = new ArrayList(Arrays.asList(1, 2, 3, 4, 5));
List<Integer> list2 = new ArrayList(Arrays.asList(1, 2, 3, 6, 7, 8, 9));
Collection intersection = CollectionUtils.intersection(list1, list2);
交集结果是: [1, 2, 3]


List<Integer> list1 = new ArrayList(Arrays.asList(1, 2, 3, 6, 7, 8, 9));
List<Integer> list2 = new ArrayList(Arrays.asList(1, 2, 3, 6, 7, 8, 9));
Collection intersection = CollectionUtils.intersection(list1, list2);
交集结果是: [1, 2, 3, 6, 7, 8, 9]

List<Integer> list1 = new ArrayList();
List<Integer> list2 = new ArrayList();
Collection intersection = CollectionUtils.intersection(list1, list2);
交集结果是: []

List<Integer> list1 = null;
List<Integer> list2 = new ArrayList();
Collection intersection = CollectionUtils.intersection(list1, list2);
NullPointerException

List<Integer> list1 = new ArrayList();
List<Integer> list2 = null;
Collection intersection = CollectionUtils.intersection(list1, list2);
NullPointerException
List<Integer> list1 = new ArrayList(Arrays.asList(1, 2, 3, 4, 5));
List<Integer> list2 = new ArrayList(Arrays.asList(1, 2, 3, 6, 7, 8, 9));
boolean existIntersection2 = new ArrayList(list1).retainAll(list2);
true

List<Integer> list1 = new ArrayList(Arrays.asList(1, 2, 3, 6, 7, 8, 9));
List<Integer> list2 = new ArrayList(Arrays.asList(1, 2, 3, 6, 7, 8, 9));
boolean existIntersection2 = new ArrayList(list1).retainAll(list2);
false

List<Integer> list1 = new ArrayList();
List<Integer> list2 = new ArrayList();
boolean existIntersection2 = new ArrayList(list1).retainAll(list2);
false

https://blog.csdn.net/HaHa_Sir/article/details/126111603

https://release.blog.csdn.net/article/details/127851890

你可能感兴趣的:(集合,java,开发语言)