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]
相关推荐
许彰午39 分钟前
03-三种开发模式
java·架构
felixking43 分钟前
C++20 协程
开发语言·c++·协程
Rain的Java大神之路1 小时前
短信接口被狂刷怎么处理
java·运维·后端·web安全·面试·架构·xss
2602_959960922 小时前
谢飞机面试大厂:Spring Boot、JVM、Redis、Kafka、微服务与音视频搜索场景求生实录
java·jvm·spring boot·redis·面试题
Zane1994122 小时前
CAS 与原子类:Java 如何实现无锁编程
java·开发语言
略略略咯咯2 小时前
AI玩具-study
java
码农颜3 小时前
6.3 主函数流程剖析
开发语言·php
TheBestRucy3 小时前
Python 九阳神功:从零筑基到线程飞升
服务器·开发语言·网络·人工智能·python
研☆香3 小时前
聊一聊前端的常见字 关键字
开发语言·前端·javascript
xier_ran3 小时前
【infra之路】GPU 存储层次总结:L1 / L2 / HBM
java·网络·数据库