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]

相关推荐
白狐_79816 小时前
408数据结构第5章:二叉树遍历序列题——技巧、判断与真题型总结
数据结构
Forever Nore18 小时前
学完C语言力扣第一题做不来正常吗
数据结构·算法
旖旎夜光21 小时前
LeetCode 11:盛最多水的容器(双指针问题) —— 题解
数据结构·c++·算法·leetcode·双指针
不一样的故事1261 天前
芯片设计是一个高度复杂、分工精细的系统工程
数据结构·经验分享
梁辰兴1 天前
软件工程:面向数据结构的设计方法
数据结构·软件工程·梁辰兴·面向数据结构的设计方法·jackson方法·warnier方法·方法比较
白狐_7981 天前
408数据结构第7章:查找②——顺序、折半、分块查找秒杀 + 真题
数据结构·算法
hold?fish:palm1 天前
链表的基本原理和实现(C++版本)
数据结构·c++·链表
码完就睡1 天前
数据结构——树、二叉树基础概念
数据结构·算法
疯狂打码的少年1 天前
【数据结构】栈:定义、顺序栈与链式栈
数据结构·笔记
wabs6661 天前
关于图论【最短路径之Bellman_ford 算法|卡码网94.城市间货物运输的思考】
数据结构·算法·图论·卡码网·bellman_ford·求最短路径