java.util.Collection的经典集合操作 [英] Classical set operations for java.util.Collection

查看:184
本文介绍了java.util.Collection的经典集合操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有关java.util.Collection类的经典集合操作的任何内置函数吗?我的具体实现将是ArrayList,但这听起来像应该适用于所有集合的子类。我在寻找类似的东西:

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:

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

经过一番搜索,我只能找到自制的解决方案。此外,我意识到我可能会混淆一个设置的想法,一个集合的思想,不允许和允许重复分别。也许这只是Set接口的功能?

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?

如果没有人知道任何内置的功能,也许我们可以使用它作为标准实践的存储库Java设置操作代码?

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.

推荐答案

交叉是使用 Collection.retainAll code>;减法与 Collection.removeAll ;与 Collection.addAll 的联合。在每种情况下, Set 将像一个集合, List 将像一个列表。

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

作为可变对象,它们操作就位。如果要保留原始的可变对象未突变,则需要显式复制。

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

这篇关于java.util.Collection的经典集合操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆