集合操作交并补的三种Java实现

基本概念

  为了便于理解,下面首先介绍集合的三个基本操作:并集、交集和补集。

  并集:以属于A或属于B的元素为元素的集合称为AB的并(集),记作AB(或BA),读作“AB”(或“BA”),即AB={x|xA,xB}

   交集 以属于A且属于B的元素为元素的集合称为AB的交(集),记作AB(或BA),读作“AB”(或“BA”),即AB={x|xA,xB}

 

      在集合论和数学的其他分支中,存在补集的两种定义:相对补集和绝对补集。

  绝对补集:属于全集U不属于集合A的元素组成的集合称为集合A的补集,记作CuA,即CuA={x|xU,x不属于A}。补集一般指绝对补集。

        相对补集差集):若A 和B 是集合,则A 在B 中的相对补集是这样一个集合:其元素属于B但不属于A,B -A = { x| x∈B且x∉A}。

      举例:令集合A={12345}集合B={12310}差集: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中的方法,可以实现集合的交集、并集和差集,方法如下:

  1. 交集 set1.retainAll(set2);
  2. 并集 set1.addAll(set2);
  3. 差集 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

 

posted @   楼兰胡杨  阅读(3639)  评论(1编辑  收藏  举报
编辑推荐:
· 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代理技术深度解析与实战指南
点击右上角即可分享
微信分享提示