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

相关推荐
阿让啊2 小时前
C语言strtol 函数使用方法
c语言·数据结构·c++·单片机·嵌入式硬件
superlls5 小时前
(算法 哈希表)【LeetCode 349】两个数组的交集 思路笔记自留
java·数据结构·算法
Ripple123126 小时前
数据结构:顺序表与链表
数据结构·链表
一个响当当的名号7 小时前
B树,B+树,B*树(无代码)
数据结构·b树
古译汉书9 小时前
嵌入式铁头山羊stm32-ADC实现定时器触发的注入序列的单通道转换-Day26
开发语言·数据结构·stm32·单片机·嵌入式硬件·算法
野犬寒鸦9 小时前
力扣hot100:相交链表与反转链表详细思路讲解(160,206)
java·数据结构·后端·算法·leetcode
GalaxyPokemon10 小时前
LeetCode - 1089. 复写零
数据结构
失散1310 小时前
分布式专题——1.2 Redis7核心数据结构
java·数据结构·redis·分布式·架构
zzzsde11 小时前
【数据结构】强化训练:从基础到入门到进阶(1)
数据结构
奔跑吧 android11 小时前
【linux kernel 常用数据结构和设计模式】【数据结构 3】【模拟input子系统input_dev和input_handler之间的多对多关系】
linux·数据结构·input·kernel·input_dev·input_handler·input_handle