Complemento de colección de listas establecido en Java

Intersección Intersección Inglés [ˌɪntəˈsekʃn]
Unión Inglés [ˈjuːniən]
Conjunto de diferencias diferencia de conjunto
complemento conjunto Inglés [ˈkɒmplɪment]
Conjunto de listas en Java toma intersección Conjunto establecido en Java toma unión Conjunto establecido
en Java toma diferencia Conjunto
establecido en Java toma conjunto de diferencias
Lista establecida en Complemento Java


# 求两个集合交集的补集
List<Integer> list1 = new ArrayList(Arrays.asList(1, 2, 3));
List<Integer> list2 = new ArrayList(Arrays.asList(3, 6, 7, 8, 9));

List<Integer> list3 = new ArrayList(list1);
list3.removeAll(list2);
List<Integer> list4 = new ArrayList(list2);
list4.removeAll(list1);
list3.addAll(list4);

list1 :[1, 2, 3]
list2 :[3, 6, 7, 8, 9]
求两个集合交集的补集:[1, 2, 6, 7, 8, 9]
# 求两个集合交集的补集
List<Integer> list1 = new ArrayList(Arrays.asList(1, 2, 3));
List<Integer> list2 = new ArrayList(Arrays.asList(3, 6, 7, 8, 9));

Collection s1 = CollectionUtils.subtract(list1, list2);
Collection s2 = CollectionUtils.subtract(list2, list1);
Collection union = CollectionUtils.union(s1, s2);

list1 :[1, 2, 3]
list2 :[3, 6, 7, 8, 9]
求两个集合交集的补集:[1, 2, 6, 7, 8, 9]
# 求集合list1相对于List1和list2全集的补集
List<Integer> list1 = new ArrayList(Arrays.asList(1, 2, 3));
List<Integer> list2 = new ArrayList(Arrays.asList(3, 6, 7, 8, 9));

List<Integer> union = new ArrayList(list1);
union.addAll(list2);
union.removeAll(list1);

list1 :[1, 2, 3]
list2 :[3, 6, 7, 8, 9]
list1 的补集:[6, 7, 8, 9]

Supongo que te gusta

Origin blog.csdn.net/weixin_37646636/article/details/132713502
Recomendado
Clasificación