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]]

相关推荐
liu****36 分钟前
10.queue的模拟实现
开发语言·数据结构·c++·算法
shinelord明1 小时前
【大数据技术实战】Kafka 认证机制全解析
大数据·数据结构·分布式·架构·kafka
让我们一起加油好吗1 小时前
【基础算法】01BFS
数据结构·c++·算法·bfs·01bfs
草莓工作室1 小时前
数据结构3:线性表2-顺序存储的线性表
数据结构·windows
1白天的黑夜12 小时前
递归-24.两两交换链表中的节点-力扣(LeetCode)
数据结构·c++·leetcode·链表·递归
1白天的黑夜12 小时前
递归-206.反转链表-力扣(LeetCode)
数据结构·c++·leetcode·链表·递归
靠近彗星2 小时前
3.1 栈
数据结构·算法
孤廖4 小时前
吃透 C++ 栈和队列:stack/queue/priority_queue 用法 + 模拟 + STL 标准实现对比
java·开发语言·数据结构·c++·人工智能·深度学习·算法
豆沙沙包?5 小时前
2025年--Lc197-077. 排序链表(链表,尾插法)--Java版
java·数据结构·链表
杨福瑞5 小时前
C语言数据结构:算法复杂度(2)
c语言·开发语言·数据结构