java.util.Collection 的经典集合操作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/163998/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 09:58:22  来源:igfitidea点击:

Classical set operations for java.util.Collection

javacollectionsset

提问by Ross

Is there any built-in functionality for classical set operations on the java.util.Collection class? My specific implementation would be for ArrayList, but this sounds like something that should apply for all subclasses of Collection. I'm looking for something like:

java.util.Collection 类的经典集合操作是否有任何内置功能?我的具体实现是针对 ArrayList,但这听起来应该适用于 Collection 的所有子类。我正在寻找类似的东西:

ArrayList<Integer> setA ...
ArrayList<Integer> setB ...
ArrayList<Integer> setAintersectionB = setA.intersection(setB);
ArrayList<Integer> setAminusB = setA.subtract(setB);

After some searching, I was only able to find home-grown solutions. Also, I realize I may be confusing the idea of a "Set" with the idea of a "Collection", not allowing and allowing duplicates respectively. Perhaps this is really just functionality for the Set interface?

经过一番搜索,我只能找到自己开发的解决方案。另外,我意识到我可能将“集合”的概念与“集合”的概念混淆,分别不允许和允许重复。也许这真的只是 Set 接口的功能?

In the event that nobody knows of any built-in functionality, perhaps we could use this as a repository for standard practice Java set operation code? I imagine this wheel has been reinvented numerous times.

如果没有人知道任何内置功能,也许我们可以将其用作标准实践 Java 集操作代码的存储库?我想这个轮子已经被重新发明了很多次。

采纳答案by Tom Hawtin - tackline

Intersection is done with Collection.retainAll; subtraction with Collection.removeAll; union with Collection.addAll. In each case, as Setwill act like a set and a Listwill act like a list.

交集是用Collection.retainAll; 减法Collection.removeAll; 与Collection.addAll. 在每种情况下, asSet都像一个集合,而 aList像一个列表。

As mutable objects, they operate in place. You'll need to explicitly copy if you want to retain the original mutable object unmutated.

作为可变对象,它们就地操作。如果您想保留原始可变对象不变,则需要显式复制。

回答by PhiLho

Are you looking for java.util.Setinterface (and its implementations HashSet and TreeSet (sorted))?
The interface defines removeAll(Collection c) which looks like substract(), and retainAll(Collection c) which looks like intersection.

您是否在寻找java.util.Set接口(及其实现 HashSet 和 TreeSet(已排序))?
该接口定义了看起来像 substract() 的 removeAll(Collection c) 和看起来像交集的retainAll(Collection c)。

回答by Benno Richters

I would recommend Google Guava. The Setsclass seems to have exactly what you are looking for. It has a intersectionmethod and a differencemethod.

我会推荐谷歌番石榴。该集合类似乎有你在寻找什么。它有交集法和差集法。

This presentationis probably something you want to watch if you're interested. It refers to Google Collections, which was Guava's original name.

如果您有兴趣,此演示文稿可能是您想要观看的内容。它指的是 Google Collections,这是 Guava 的原始名称。

回答by mhstnsc

For mutable operations see accepted answer.

对于可变操作,请参阅已接受的答案。

For an imutable variant you can do this with java 8

对于不可变变体,您可以使用 java 8 执行此操作

subtraction

减法

set1
  .stream()
  .filter(item-> !set2.contains(item))
  .collect(Collectors.toSet())

intersection

路口

set1
  .stream()
  .filter(item-> set2.contains(item))
  .collect(Collectors.toSet())