list交并补差集合

list交并补差集合

工具类依赖

xml 复制代码
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.8.1</version>
</dependency>

<dependency>
    <groupId>commons-collections</groupId>
	<artifactId>commons-collections</artifactId>
	<version>3.2</version>
</dependency>

参考代码

java 复制代码
		// 交并补差
        List<Integer> list1 = Lists.newArrayList(1,2,3);
        List<Integer> list2 = Lists.newArrayList(3,4,5);
        List<Integer> intersection = (List<Integer>) CollectionUtils.intersection(list1, list2);
        log.info("list1与list2交集:[{}]",intersection);

        List<Integer> unionResult = (List<Integer>) CollectionUtils.union(list1, list2);
        log.info("list1与list2并集:[{}]",unionResult);

        List<Integer> subtractList = (List<Integer>) CollectionUtils.subtract(list1, list2);
        log.info("list1与list2差集:[{}]",subtractList);

        List<Integer> subtractListSecond = (List<Integer>) CollectionUtils.subtract(list2, list1);
        log.info("list2与list1差集:[{}]",subtractListSecond);

        List<Integer> disjunctionList = (List<Integer>) CollectionUtils.disjunction(list1, list2);
        log.info("list1与list2补集:[{}]",disjunctionList);

00:05:52.126 main INFO com.geekmice.sbeasypoi.service.impl.ds - list1与list2交集:\[3]

00:05:52.136 main INFO com.geekmice.sbeasypoi.service.impl.ds - list1与list2并集:\[1, 2, 3, 4, 5]

00:05:52.147 main INFO com.geekmice.sbeasypoi.service.impl.ds - list1与list2差集:\[1, 2]

00:05:52.147 main INFO com.geekmice.sbeasypoi.service.impl.ds - list2与list1差集:\[4, 5]

00:05:52.149 main INFO com.geekmice.sbeasypoi.service.impl.ds - list1与list2补集:\[1, 2, 4, 5]

相关推荐
Darling噜啦啦3 天前
列表转树算法深度解析:从 Map 到 Reduce 的两种实现,面试高频考点
数据结构·算法·面试
小小工匠4 天前
Redis - 事务机制:能实现 ACID 属性吗
数据结构·redis·性能优化·并发·持久化
玖玥拾4 天前
C/C++ 数据结构(七)栈、容器适配器
c语言·数据结构·c++··容器适配器
Qres8214 天前
算法复键——树状数组
数据结构·算法
牛油果子哥q4 天前
并查集(DSU)超精讲,路径压缩、按秩合并、万能模板、连通性判定、最小生成树与刷题实战全解
数据结构·c++·最小生成树·并查集
凌波粒4 天前
LeetCode--491.递增子序列(回溯算法)
数据结构·算法·leetcode
世人万千丶4 天前
成语接龙小应用 - HarmonyOS ArkUI 开发实战-TextInput与List列表-PC版本
华为·list·harmonyos·鸿蒙·鸿蒙系统
WL学习笔记4 天前
单项不带头不循环链表
数据结构·链表
小糯米6014 天前
JS 数组
数据结构·算法·排序算法
未若君雅裁4 天前
Python 数据容器详解,list、tuple、str、set、dict 到底怎么选
windows·python·list