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]

相关推荐
UIU1141 小时前
P16237 [蓝桥杯 2026 省 B] 应急布线
数据结构·c++·算法·蓝桥杯
黎相思4 小时前
BTree
数据结构·b树·b+树
UIU11415 小时前
P1233 [ICPC 2001 Taejon R] 木棍加工
数据结构·c++·算法·动态规划
niyongsheng18 小时前
3D 装箱算法工程化:数据结构、放置启发式与支撑规则
数据结构·算法·rust
叉烧滑蛋20 小时前
B站台湾大学郭彦甫|MATLAB 学习笔记|04 变量与档案存取
数据结构
j7~20 小时前
【穷举、深搜与回溯算法】一棵决策树讲透全排列与子集
数据结构·c++·算法·leetcode·回溯·递归
not coder1 天前
一文讲通--从树到红黑树
数据结构·算法
爱吃香菜的初学者1 天前
八.数据结构——哈希表(1)
数据结构·散列表
kiracrimson1 天前
STL:List的使用和模拟实现
开发语言·c++·list
二十七日秋1 天前
算法刷题笔记「Day 1|四道 OJ 入门题里的深坑」
c语言·汇编·数据结构·c++·笔记·python·算法