java Java如何快速比较Java中两个完全相同的集合?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12649178/
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
How does Java quickly compare two collection are exactly the same in Java?
提问by Liu guanghua
How does Java quickly compare two collection are exactly the same in java?
Java如何快速比较java中两个完全相同的集合?
for example:
例如:
if this collection is Set, compare their contain the same object and their size are same.
如果此集合为 Set,则比较它们包含相同的对象并且它们的大小相同。
Code looks like the following
代码如下所示
public boolean isSameSets(Set<T> set1, Set<T> set2){
if (set1.size() != set2.size()) {
return false;
}
return set1.containAll(set2) && set2.containAll(set1);
}
But if the collection is list, because list is sorted collection, we can iterate to compare every element, I think this way is not the best, who could please tell me how to quickly compare them?
但是如果集合是列表,因为列表是排序集合,我们可以迭代比较每个元素,我认为这种方式不是最好的,谁能告诉我如何快速比较它们?
========================================================
================================================== ======
Thanks everyone, actually, Collection equals method can do it, include Set and List.
谢谢大家,其实Collection equals方法可以做到,包括Set和List。
回答by Stephen C
The Collection equals(Object)
method should do this for you. The javadocexplains exactly what "equals" means for a Collection, and the semantics are further refined for the interfaces that extend Collection.
Collectionequals(Object)
方法应该为您执行此操作。该javadoc的解释正是“等于”为收集装置,以及语义进一步细化为扩展集合的接口。
Different collection classes will have their own implementations of this method, tuned for their respective semantic models and representations. So for instance, the equals
method on a List
would take account of the element order, but the equals
method on a Set
would typically not.
不同的集合类将有自己的此方法实现,针对各自的语义模型和表示进行调整。因此,例如, a 上的equals
方法List
会考虑元素顺序,但 a 上的equals
方法Set
通常不会。
Someone asks:
有人问:
cant we use containsAll(Collection).
我们不能使用 containsAll(Collection)。
In general, no. For instance, two lists that have the same elements are not necessarily equal. You have to also consider list order. (And besides, using containsAll
to compare lists is O(N^2)
where an efficient equals
implementation for a List
should be O(N)
... in the worst case.)
一般来说,没有。例如,具有相同元素的两个列表不一定相等。您还必须考虑列表顺序。(此外,在最坏的情况下,使用containsAll
比较列表是aO(N^2)
的有效equals
实现List
应该是O(N)
......。)
The same applies to using retainsAll
.
这同样适用于使用retainsAll
.
回答by Tim Pote
The HashSet.equals
method already does comparisons to make sure there are the exact same elements in each set. The ArrayList.equals
does the same except it also checks ordering.
该HashSet.equals
方法已经进行了比较,以确保每个集合中有完全相同的元素。在ArrayList.equals
做同样的,除了它也检查顺序。
回答by gtgaxiola
You can use Collection.retainAll()
回答by Alexandr
Also consider using Google Guava library:
还可以考虑使用 Google Guava 库:
elementsEqual(Iterable, Iterable)
Returns true if the iterables have the same elements in the same order.
Even if collections are HashSet where the order is not predifined, but the number of elements is the same and the elements are equal, during iterating you'll find out that the order of both sets is the same, provided with correct hashCode
and equals
methods.
如果可迭代对象具有相同顺序的相同元素,则返回 true。即使集合是HashSet的其中顺序不predifined,但元件的数量是相同的,元素是相等的,迭代过程中你会发现,这两组的顺序是一样的,提供正确hashCode
和equals
方法。