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

前言

本文主要给大家简单介绍下,如何使用apache commons以及guava的类库来计算集合的交差并集。分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍吧。

maven

  
   com.google.guava
   guava
   22.0
  
  
   org.apache.commons
   commons-collections4
   4.1
  

guava

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

commons-collection4

  //交集
  Collection intersectionSet = CollectionUtils.intersection(set1, set2);
  //差集
  Collection subtractSet = CollectionUtils.subtract(set1, set2);
  //并集
  Collection unionSet = CollectionUtils.union(set1, set2);

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对脚本之家的支持。

你可能感兴趣的:(java中计算集合的交差并集示例代码)