java计算集合交差并集

这里简单介绍下,如何使用apache commons以及guava的类库来计算集合的交差并集。

maven

        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>22.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-collections4</artifactId>
            <version>4.1</version>
        </dependency>

guava

        //交集
        Set<Integer> intersectionSet = Sets.intersection(set1, set2);
        //差集
        Set<Integer> subtractSet = Sets.difference(set1, set2);
        //并集
        Set<Integer> unionSet = Sets.union(set1, set2);

commons-collection4

        //交集
        Collection<Integer> intersectionSet = CollectionUtils.intersection(set1, set2);
        //差集
        Collection<Integer> subtractSet = CollectionUtils.subtract(set1, set2);
        //并集
        Collection<Integer> unionSet = CollectionUtils.union(set1, set2);
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,970评论 19 139
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 176,036评论 25 709
  • spring官方文档:http://docs.spring.io/spring/docs/current/spri...
    牛马风情阅读 5,726评论 0 3
  • 依稀还记得校贴吧曾经轰动一时的那件事。男生发帖求助,晚上从实验室出来,发现从老家带来准备给女朋友的5000元彩礼钱...
    书悦阅读 8,927评论 0 0
  • 余也不知道自己的善解人意到底是不是自己这么敏感的缘由。 余上中学的时候就住在了学校,室友也都是老实人,刚开始跟...
    权慧阅读 1,598评论 0 2

友情链接更多精彩内容