集合操作交并补的三种Java实现
基本概念
为了便于理解,下面首先介绍集合的三个基本操作:并集、交集和补集。
并集:以属于A或属于B的元素为元素的集合称为A与B的并(集),记作A∪B(或B∪A),读作“A并B”(或“B并A”),即A∪B={x|x∈A,或x∈B}。
交集: 以属于A且属于B的元素为元素的集合称为A与B的交(集),记作A∩B(或B∩A),读作“A交B”(或“B交A”),即A∩B={x|x∈A,且x∈B}。
在集合论和数学的其他分支中,存在补集的两种定义:相对补集和绝对补集。
绝对补集:属于全集U不属于集合A的元素组成的集合称为集合A的补集,记作CuA,即CuA={x|x∈U,且x不属于A}。补集一般指绝对补集。
相对补集(差集):若A 和B 是集合,则A 在B 中的相对补集是这样一个集合:其元素属于B但不属于A,B -A = { x| x∈B且x∉A}。
举例:令集合A={1,2,3,4,5},集合B={1,2,3,10},则差集:B-A={10}。
在介绍集合实现方法之前,先介绍一下本文要用到的包:
import com.google.common.collect.Lists; import com.google.common.collect.Maps; import com.google.common.collect.Sets; import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.lang3.StringUtils; import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner;
本文基于Java 8进行测试,采用的Guava版本为
<dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>29.0-jre</version> </dependency>
Jdk中的操作
定义两个Integer类型的非空集合set1和set2,则在使用JDK中的方法,可以实现集合的交集、并集和差集,方法如下:
- 交集 set1.retainAll(set2);
- 并集 set1.addAll(set2);
- 差集 or 补集 set1.removeAll(set2)。
温馨提示,它们都是把结果集记录在set1中,使用的时候需要留意。
Guava Sets操作
@Test
public void setOperation() {
Set<Integer> set1 = Sets.newHashSet(1, 2, 3, 4, 5);
Set<Integer> set2 = Sets.newHashSet(3, 4, 5, 6, 7, 9);
Sets.SetView<Integer> intersection = Sets.intersection(set1, set2);
LOGGER.info("交集为:{}", intersection.toString());//[3, 4, 5]
Sets.SetView<Integer> union = Sets.union(set1, set2);
LOGGER.info("并集为:{}", union.toString());//[1, 2, 3, 4, 5, 6, 7, 9]
Sets.SetView<Integer> diff = Sets.difference(set1, set2);
LOGGER.info("差集为:{}", diff.toString());//[1, 2]
set1.retainAll(set2);
LOGGER.info("jdk中求交集:{}", set1);
}
Apache CollectionUtils
org.apache.commons.collections4.CollectionUtils同样实现了集合的交集、并集和差集,方法如下:
@Test
public void CollectionUtilsOperation() {
Set<Integer> set1 = Sets.newHashSet(1, 2, 3, 4, 5);
Set<Integer> set2 = Sets.newHashSet(3, 4, 5, 6, 7, 9);
Collection<Integer> col = CollectionUtils.retainAll(set1, set2);
LOGGER.info("交集为:{}", col.toString());
col = CollectionUtils.union(set1, set2);
LOGGER.info("并集为:{}", col.toString());
col = CollectionUtils.subtract(set1, set2);
LOGGER.info("补集为:{}", col.toString());
}
List 取交集
使用jdk自带的轮子retainAll求两个List的交集,demo:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | public static void main(String[] args) { List<String> sourceList = new ArrayList<>(); List<String> candidateList = new ArrayList<>(); sourceList.add( "10" ); sourceList.add( "2" ); sourceList.add( "3" ); sourceList.add( "7" ); candidateList.add( "10" ); candidateList.add( "3" ); candidateList.add( "32" ); sourceList.retainAll(candidateList); System.out.println( "两个List的交集:" ); for (String s : sourceList){ System.out.println(s); } } |
这是List自带的retain取交集的方法,执行后控制台打印结果如下:
1 2 3 | 两个List的交集: 10 3 |
Reference
https://zhidao.baidu.com/question/116778634.html

【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南