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]

相关推荐
码完就睡10 小时前
数据结构——红黑树
数据结构
码爸11 小时前
数据结构详细介绍
数据结构
CQU_JIAKE14 小时前
10.9【A】
数据结构
第25小时记录14 小时前
数据结构与算法 -第 3 章 常用算法-动态规划
数据结构·python
Logic10118 小时前
C语言/数据结构数位DP题解:平滑排列数字统计——区间内相邻位差不超过K的数字个数
c语言·数据结构·动态规划·时间复杂度·数位dp·算法题·前导零处理
大鹏说大话19 小时前
C++ STL 容器怎么选?vector、list、map、unordered_map 对比
开发语言·c++·list
wang090719 小时前
硬着头皮学数据结构和算法之Myers算法
数据结构·算法
垆边人似月.20 小时前
华为机试题 :最长递增子序列题目
数据结构·c++·算法·华为
yi0111 天前
LeetCode 219:存在重复元素 II——哈希表记录“最近一次出现的位置”
数据结构·人工智能·笔记·python·算法·leetcode·哈希表