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

相关推荐
Sam_Deep_Thinking19 小时前
学数据结构到底有什么用
数据结构
py有趣1 天前
力扣热门100题之和为K的子数组
数据结构·算法·leetcode
hipolymers1 天前
C语言怎么样?难学吗?
c语言·数据结构·学习·算法·编程
CS创新实验室1 天前
从“跑得动”到“跑得稳”:深度剖析数据结构究竟是理论点缀还是核心战力?
数据结构
jllllyuz1 天前
MATLAB 蒙特卡洛排队等待模拟程序
数据结构·matlab
自我意识的多元宇宙1 天前
树、森林——树、森林与二叉树的转换(森林转换为二叉树)
数据结构
海清河晏1111 天前
数据结构 | 双循环链表
数据结构·链表
py有趣1 天前
力扣热门100题之编辑距离
数据结构·算法·leetcode
努力努力再努力wz1 天前
【Linux网络系列】万字硬核解析网络层核心:IP协议到IP 分片重组、NAT技术及 RIP/OSPF 动态路由全景
java·linux·运维·服务器·数据结构·c++·python
谭欣辰1 天前
AC自动机:多模式匹配的高效利器
数据结构·c++·算法