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]

相关推荐
en.en..5 小时前
冒泡排序与选择排序完整对比解析
开发语言·数据结构·算法·c#·排序算法
什巳7 小时前
JAVA练习312- 二叉搜索树中第 K 小的元素
java·数据结构·算法·leetcode
罗超驿10 小时前
9.优先级队列与堆:从底层原理到实战应用的完整指南
数据结构·算法
牧以南歌〆11 小时前
数据结构<三>单循环链表
c语言·数据结构·算法·链表
Java技术实战12 小时前
一文通解:数据结构之 栈
java·jvm·数据结构·
江屿风12 小时前
【C++笔记】List流食般投喂
开发语言·c++·笔记·list
Lucis__15 小时前
基于Cache替换算法的LRU缓存实现
数据结构·c++·算法·缓存·lru
来一碗刘肉面1 天前
顺序表与链表的比较
数据结构·算法·链表
txzrxz1 天前
二分图详解
数据结构·c++·算法·图论·深度优先搜索·二分图染色
fpcc1 天前
数据结构和算法—数学的应用
数据结构·c++·算法