Java使用不同方式获取两个集合List的交集、补集、并集(相加)、差集(相减)

2023-05-16

1 明确概念

首先知道几个单词的意思:

并集 = union

交集 = intersection

补集 = complement

析取 = disjunction

减去 = subtract

1.1 并集

对于两个给定集合A、B,由两个集合所有元素构成的集合,叫做A和B的并集。

记作:AUB 读作“A并B”

例:{3,5}U{2,3,4,6}= {2,3,4,5,6}

1.2 交集

对于两个给定集合A、B,由属于A又属于B的所有元素构成的集合,叫做A和B的交集。

记作: A∩B 读作“A交B”

例:A={1,2,3,4,5},B={3,4,5,6,8},A∩B={3,4,5}

1.3 补集

一般地,设S是一个集合,A是S的一个子集,由S中所有不属于A的元素组成的集合,叫做子集A在S中的绝对补集。

记作:∁UA,包括三层含义:

1)A是U的一个子集,即A⊊U;

2)∁UA表示一个集合,且∁UA⊊U;

3)∁UA是由U中所有不属于A的元素组成的集合,∁UA与A没有公共元素,U中的元素分布在这两个集合中。

2 使用apache工具包

2.1 导入依赖

        <!-- apache 集合工具类 -->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-collections4</artifactId>
            <version>4.1</version>
        </dependency>

2.2 代码如下:

    public static final List<String> list1 = Arrays.asList("A", "B", "C", "D", "E", "F", null);
    public static final List<String> list2 = Arrays.asList("1", "2", "3", "D", "E", "F", null);

    /**
     * apache测试方法
     */
    @Test
    public void apacheTest() {
        System.out.println("交集:" + CollectionUtils.intersection(list1, list2)); // 交集
        System.out.println("补集:" + CollectionUtils.disjunction(list1, list2)); // 补集
        System.out.println("并集:" + CollectionUtils.union(list1, list2)); // 并集
        System.out.println("list1的差集:" + CollectionUtils.subtract(list1, list2)); // list1的差集
        System.out.println("list2的差集:" + CollectionUtils.subtract(list2, list1)); // list2的差集
    }

输出:

3 使用hutool工具包

3.1 导入依赖

        <!-- hutool工具类 -->
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.8.12</version>
        </dependency>

3.2 代码如下:

    public static final List<String> list1 = Arrays.asList("A", "B", "C", "D", "E", "F", null);
    public static final List<String> list2 = Arrays.asList("1", "2", "3", "D", "E", "F", null);

    /**
     * hutool工具类
     */
    @Test
    public void hutoolTest() {
        System.out.println("交集:" + CollectionUtil.intersection(list1, list2)); // 交集
        System.out.println("补集:" + CollectionUtil.disjunction(list1, list2)); // 补集
        System.out.println("并集:" + CollectionUtil.union(list1, list2)); //并集
        System.out.println("list1的差集"+CollectionUtil.subtract(list1,list2));
        System.out.println("list2的差集"+CollectionUtil.subtract(list2,list1));
        System.out.println("list1的差集:" + CollectionUtil.subtractToList(list1, list2));
        System.out.println("list2的差集:" + CollectionUtil.subtractToList(list2, list1));
    }

输出:

3.3 注意

subtract()和subtractToList()作用一样,不过处理方式不同,使用subtract()时,参数不能是Arrays.asList()的结果,否则会报异常,因为Arrays.asList()返回的对象是Arrays.ArrayList,在方法实现里面调用的是AbstractList抽象类里面的removeAll()方法,该方法代码如下:

public abstract class AbstractList<E> extends AbstractCollection<E> implements List<E> {
 
    // ....
 
    public E remove(int index) {
        throw new UnsupportedOperationException();
    }
 
    // ....
}

4 使用stream方式

4.1 代码如下:

    public static final List<String> list1 = Arrays.asList("A", "B", "C", "D", "E", "F", null);
    public static final List<String> list2 = Arrays.asList("1", "2", "3", "D", "E", "F", null);

    /**
     * stream方式
     */
    @Test
    public void streamTest() {
        List<Object> intersection = list1.stream().filter(list2::contains).collect(Collectors.toList());
        System.out.println("交集:" + intersection);

        List<String> subtract1 = list1.stream().filter(s -> !list2.contains(s)).collect(Collectors.toList());
        System.out.println("集合list1的差集:" + subtract1);
        List<String> subtract2 = list2.stream().filter(s -> !list1.contains(s)).collect(Collectors.toList());
        System.out.println("集合list2的差集:" + subtract2);

        List<String> union1 = list1.parallelStream().collect(Collectors.toList());
        List<String> union2 = list2.parallelStream().collect(Collectors.toList());
        union1.addAll(union2);
        List<String> union3 = union1.stream().distinct().collect(Collectors.toList());
        System.out.println("并集:" + union3);
    }

输出:

5 使用Collection接口中的方法

5.1 代码如下:

    public static String[] attr1 = new String[]{"A", "B", "C", "D", "E", "F", null};
    public static String[] attr2 = new String[]{"1", "2", "3", "D", "E", "F", null};

    /**
     * 使用Collection接口中的方法
     */
    @Test
    public void collectionTest() {
        List<String> list1 = new ArrayList<>(Arrays.asList(attr1));
        List<String> list2 = new ArrayList<>(Arrays.asList(attr2));
        list1.retainAll(list2);
        System.out.println("交集:" + list1);

        ArrayList<String> list3 = new ArrayList<>(Arrays.asList(attr1));
        ArrayList<String> list4 = new ArrayList<>(Arrays.asList(attr2));
        Set<Object> set = new HashSet<>();
        set.addAll(list3);
        set.addAll(list4);
        System.out.println("并集:" + set);

        ArrayList<String> list5 = new ArrayList<>(Arrays.asList(attr1));
        ArrayList<String> list6 = new ArrayList<>(Arrays.asList(attr2));
        list5.removeAll(list6);
        System.out.println("集合A的差集:" + list5);
        ArrayList<String> list7 = new ArrayList<>(Arrays.asList(attr1));
        ArrayList<String> list8 = new ArrayList<>(Arrays.asList(attr2));
        list8.removeAll(list7);
        System.out.println("集合B的差集:" + list8);
    }

输出:

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Java使用不同方式获取两个集合List的交集、补集、并集(相加)、差集(相减) 的相关文章

随机推荐

  • java swing 常用的三种布局方式:边界布局、流布局、网格布局管理器

    作者 xff1a firstmiki 链接 xff1a http www cnblogs com firstmiki p 6340001 html 来源 xff1a firstmiki的博客 著作权归作者所有 xff0c 转载请联系作者获得
  • Linux(CentOS 7)配置静态ip及ping 不通外网问题

    前言 日常学习中 xff0c 如果Linux中安装了MySQL nacos redis等中间件 我们可能会通过navicat dataGrip连接MySQL 会通过ip port nacos访问nacos 会通过ip port连接redis
  • docker run启动镜像容器时忘记添加开机自启动解决方法

    问题描述 在使用以下命令启动mysql容器时 xff0c 忘记添加了 restart 61 always 开启开机自启动 xff0c 导致每次开机 重启后 xff0c 需要重新通过一系列命令手动重启对应的容器 xff0c 极其不方便 doc
  • docker 安装RabbitMQ

    系列文章目录 第一章 RabbitMQ安装 提示 xff1a 写完文章后 xff0c 目录可以自动生成 xff0c 如何生成可参考右边的帮助文档 文章目录 系列文章目录前言一 RabbitMQ安装1 dockerHub 选择镜像2 Cent
  • 记 Content type ‘application/octet-stream‘ not supported

    项目场景 xff1a 实现一个入参方式为 64 RequestPart 43 64 RequestParam files 的接口 xff0c 即该接口要包含文件上传和其它 实体类 入参 示例代码 xff1a 64 PostMapping v
  • Go语言环境搭建

    一 下载开发工具 1 1 Go语言官网 1 2 进入Go官网 xff0c 点击Download xff0c 进入开发工具下载界面 xff0c 根据个人系统选择对应的安装包进行下载 windows 对应下载链接 二 安装开发工具 2 1 双击
  • Go入门教程--Hello,World.

    零 文本编辑器实现Hello World 官方给出的教程主要针对Linux 或Mac系统 但也没关系 xff0c 在windows 中实现也很简单 0 1 创建文件夹 xff0c 名称任意 xff0c 如 xff1a hello 0 2 创
  • 在AWS上开通EC2服务器并部署tomcat

    1 登录aws 2 点击服务 计算 EC2 3 点击启动实例 4 选择linux镜像 5 选择一个实例类型 6 配置实例详细信息 xff0c 保持默认 xff0c 点击下一步 7 添加存储 xff08 选择linux根目录硬盘大小和类型 x
  • android studio gradle 使用阿里源 (修改 settings.gradle)

    默认的地址下载速度极慢 依赖项几个小时也下载不完 改为 阿里源 1分钟就下载ok了 代码 修改根目录中 的 settings gradle 文件 内容 pluginManagement span class token punctuatio
  • 实用!Windows 远程控制 Ubuntu 系统

    点击上方 xff0c 选择 设为星标 优质文章 xff0c 及时送达 上一篇 xff1a 来源 xff1a 头条 互联网上的小蜘蛛 有时需要在实际的电脑上安装Ubuntu的操作系统来搭建免费的网站平台 这就需要使用远程的客户端Windows
  • 并查集——洛谷P3367

    题目描述 如题 xff0c 现在有一个并查集 xff0c 你需要完成合并和查询操作 输入输出格式 输入格式 xff1a 第一行包含两个整数N M xff0c 表示共有N个元素和M个操作 接下来M行 xff0c 每行包含三个整数Zi Xi Y
  • Web项目通过webservice编写一个接口,部署在远程服务器上

    在我的上一片文章中 xff0c 我在本地新建了一个普通的类来编写WebService xff0c 使用终端类 Endpoint 发布这个WebService xff0c 以此来实现让其他类调用这个接口 xff0c 实现接口中定义的功能 通过
  • ubuntu与win10共享LE蓝牙鼠标

    类似的教程网上有很多 xff0c 大部分是找到蓝牙设备目录下info文件中的 linkKey 中的key值复制到win10下注册表中 xff0c 但是对于蓝牙5 0或LE设备来说 xff0c 是没有linKey的 xff0c 这里我也参考了
  • FileZilla搭建FTP服务器图解教程,并允许外网访问NAT内网

    FTP是用来在两台计算机之间传输文件 xff0c 是Internet中应用非常广泛的服务之一 FTP服务是网络中经常采用的资源共享方式之一 FTP协议有PORT和PASV两种工作模式 xff0c 即主动模式和被动模式 今天我分享一个最近我自
  • 十进制转换八进制(C语言基础)

    题目描述编程 xff0c 输入一个 xff11 xff10 进制正整数 xff0c 然后输出它所对应的八进制数 输入无输出无样例输入10样例输出12 include lt stdio h gt int main int num m 61 0
  • 【Godot】对 Godot 节点设计的思考

    对 Godot 中节点设计的思考 单个节点的功能设计的想法 xff0c 体会 Godot 的设计思想 低耦合 设计单个节点可复用的节点时 xff0c 调用方法尽量只对当前节点可获取到的变量或方法进行使用 xff0c 比如我写一个可以控制 K
  • 【Godot】行为树(一)了解与设计行为树代码

    行为树介绍 行为树是个节点树 xff0c 父节点通过不断遍历子节点 xff0c 根据不同类型的节点执行不同的分支 最终调用叶节点执行功能 行为树也不难理解 xff0c 他就像代码逻辑一样 xff0c 只是用节点的方式展现出来 xff0c 而
  • 【Godot 4.0】一个简单的匿名方法的使用lambda

    Godot 4 0 beta3 Godot 4 0 中添加了 lambda 表达式 xff0c 匿名方法等很多方便的特性 xff0c 这里我写个用于扫描目录下所有文件的功能 可以看到代码非常简洁 span class token keywo
  • aur报错(错误:一个或多个文件没有通过有效性检查)

    当我们从aur里安装软件时 xff0c 有时会出现这种报错 xff08 如安装deepin wine wechat xff09 61 61 gt 错误 xff1a 一个或多个文件没有通过有效性检查 xff01 Error downloadi
  • Java使用不同方式获取两个集合List的交集、补集、并集(相加)、差集(相减)

    1 明确概念 首先知道几个单词的意思 xff1a 并集 61 union 交集 61 intersection 补集 61 complement 析取 61 disjunction 减去 61 subtract 1 1 并集 对于两个给定集