Java 中 List 集合取补集

交集 Intersection 英 [ˌɪntəˈsekʃn]
并集 Union 英 [ˈjuːniən]
差集 difference of set
补集 complement set 英 [ˈkɒmplɪment]
Java 中 List 集合取交集
Java 中 List 集合取并集
Java 中 List 集合取差集
Java 中 List 集合取补集


csharp 复制代码
# 求两个集合交集的补集
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]
csharp 复制代码
# 求两个集合交集的补集
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]
csharp 复制代码
# 求集合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]
相关推荐
dingdingfish6 小时前
Bash学习 - 第3章:Basic Shell Features,第5节:Shell Expansions
开发语言·学习·bash
rainbow68896 小时前
C++开源库dxflib解析DXF文件实战
开发语言·c++·开源
不倒翁玩偶6 小时前
IDEA导入新的SpringBoot项目没有启动按钮
java·spring boot·intellij-idea
deepxuan6 小时前
Day7--python
开发语言·python
小小小米粒6 小时前
Maven Tools
java
禹凕6 小时前
Python编程——进阶知识(多线程)
开发语言·爬虫·python
蜡笔小马7 小时前
10.Boost.Geometry R-tree 空间索引详解
开发语言·c++·算法·r-tree
IOsetting7 小时前
金山云主机添加开机路由
运维·服务器·开发语言·网络·php
kali-Myon7 小时前
2025春秋杯网络安全联赛冬季赛-day1
java·sql·安全·web安全·ai·php·web
我是咸鱼不闲呀7 小时前
力扣Hot100系列20(Java)——[动态规划]总结(下)( 单词拆分,最大递增子序列,乘积最大子数组 ,分割等和子集,最长有效括号)
java·leetcode·动态规划