交集 Intersection 英 [ˌɪntəˈsekʃn]
并集 Union 英 [ˈjuːniən]
差集 difference of set
补集 complement set 英 [ˈkɒmplɪment]
Further Reading :Java 中 List 集合取交集
Further Reading :Java 中 List 集合取并集
Further Reading :Java 中 List 集合取差集
Further Reading :Java 中 List 集合取补集文章来源:https://www.toymoban.com/news/detail-704851.html
求两个集合交集的补集
方法1:
# 求两个集合交集的补集
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]
方法2:
# 求两个集合交集的补集
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]
方法3
List<Integer> list1 = new ArrayList(Arrays.asList(1, 2, 3));
List<Integer> list2 = new ArrayList(Arrays.asList(3, 6, 7, 8, 9));
Collection disjunction = CollectionUtils.disjunction(list1, list2);
求两个集合交集的补集:[1, 2, 6, 7, 8, 9]
求集合list1相对于List1和list2全集的补集
# 求集合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]
参考:
java 集合交集、并集、差集、补集1
java 集合交集、并集、差集、补集2文章来源地址https://www.toymoban.com/news/detail-704851.html
到了这里,关于Java 中 List 集合取补集的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!