温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

java中计算集合的交差并集示例代码

发布时间:2020-09-02 13:51:58 来源:脚本之家 阅读:139 作者:xixicat 栏目:编程语言

前言

本文主要给大家简单介绍下,如何使用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问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI