基于apache集合工具包的并集、交集、差集工具类

简介: 基于apache集合工具包的并集、交集、差集工具类

并集、交集、差集、补集的概念是什么?

一、依赖

    <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-collections4</artifactId>
            <version>4.4</version>
        </dependency>

二、工具类

 
import org.apache.commons.collections4.CollectionUtils;
 
import java.util.List;
 
public class ListUtils {
    //获取两个集合并集(自动去重)
    public static  <T> List<T> getUnion(List<T> list1, List<T> list2){
        List<T> union = (List<T>) CollectionUtils.union(list1, list2);
        return union;
    }
 
    //获取两个集合交集
    public static  <T> List<T> getIntersection(List<T> list1,List<T> list2){
        List<T> intersection = (List<T>) CollectionUtils.intersection(list1, list2);
        return intersection;
    }
    //获取两个集合交集的补集 即 list1 + list2 - 交集
    public static  <T> List<T> getDisjunction(List<T> list1,List<T> list2){
        List<T> disjunction = (List<T>)CollectionUtils.disjunction(list1, list2);
        return disjunction;
    }
 
    //获取两个集合的差集 list1 - 交集
    public static  <T> List<T> getSubtract(List<T> list1,List<T> list2){
        List<T> subtract = (List<T>)CollectionUtils.subtract(list1, list2);
        return subtract;
    }
}

三、测试

public static void main(String[] args) {
        List<Integer> list1 = Stream.of(1, 2, 3).collect(Collectors.toList());
        List<Integer> list2 = Stream.of(2, 3, 4).collect(Collectors.toList());
 
        System.out.println("list1:" + list1);
        System.out.println("list2:" + list2);
        List<Integer> union = getUnion(list1, list2);
        System.out.println("list1-list2并集union:" + union);
 
        List<Integer> intersection = getIntersection(list1, list2);
        System.out.println("list1,-list2交集:" + intersection);
 
        List<Integer> disjunction = getDisjunction(list1, list2);
        System.out.println("list1-list2交集的补集:" + disjunction);
 
        List<Integer> subtract = getSubtract( union,list1);
        System.out.println("union-list1差集:" + subtract);
 
    }
list1:[1, 2, 3]
list2:[2, 3, 4]
list1-list2并集union:[1, 2, 3, 4]
list1,-list2交集:[2, 3]
list1-list2交集的补集:[1, 4]
union-list1差集:[4]
目录
相关文章
|
8天前
|
Apache 数据库
杨校老师课堂之基于Apache的数据库连接池DBCP的工具类开发
杨校老师课堂之基于Apache的数据库连接池DBCP的工具类开发
11 0
|
1月前
|
Shell Linux Apache
【Shell 命令集合 网络通讯 】Linux 管理Apache HTTP服务器 apachectl命令 使用教程
【Shell 命令集合 网络通讯 】Linux 管理Apache HTTP服务器 apachectl命令 使用教程
183 1
|
1月前
|
Shell Linux 网络安全
【Shell 命令集合 网络通讯 】Linux 管理Apache HTTP服务器 httpd命令 使用指南
【Shell 命令集合 网络通讯 】Linux 管理Apache HTTP服务器 httpd命令 使用指南
46 0
|
6月前
|
Java Apache Spring
Spring BeanUtils 2、Cglib BeanCopier 3、Apache BeanUtils 4、Apache PropertyUtils 5、Dozer 那么,我们到底应该选择哪种工具类更加合适呢?为什么Java开发手册中提到禁止使用Apache BeanUtils呢
Spring BeanUtils 2、Cglib BeanCopier 3、Apache BeanUtils 4、Apache PropertyUtils 5、Dozer 那么,我们到底应该选择哪种工具类更加合适呢?为什么Java开发手册中提到禁止使用Apache BeanUtils呢
73 0
|
安全 Apache 数据安全/隐私保护
Apache Shiro < 1.7.0 权限绕过漏洞集合
今天发现阿里云服务器出现了告警: shiro 1.3.2 进程ID:3932 路径:xxx\shiro-core-1.3.2.jar 命中:shiro version less than 1.7.0 网上一搜,找到了如下资料: 2020年11月2日,阿里云应急响应中心监测到Apache Shiro官方发布安全更新,修复了一个最新权限绕过漏洞。攻击者利用该漏洞可以绕过验证访问到后台功能,风险较高
292 0
|
Java API Apache
【小家java】Java中Apache Commons-lang3提供的DateUtils/DateFormatUtils等时间、日期工具类
【小家java】Java中Apache Commons-lang3提供的DateUtils/DateFormatUtils等时间、日期工具类
|
算法 Java Apache
|
Java Apache API
Bag集合工具类(apache-commons-collections3.2工具包)在java中的使用
Bag集合工具类(apache-commons-collections3.2工具包)在java中的使用 Bag 是在 org.apache.commons.collections 包中定义的接口 ,也是集合的一种扩充工具类,当然结合用JDK中的map类进行相应的逻辑处理,也能实现Bag类的功能,但apache推出来肯定有它的原因和用处,知道有这么一个类了解它大概的用法,开发的时候真遇到这种情况,知道有这么个工具在你身边等着你用呢。
2705 0

推荐镜像

更多