java 如何有效地比较 Sets?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13360675/
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 to efficiently compare Sets?
提问by user1654885
Given two Sets: how to compare them efficiently in Java?
给定两个集合:如何在 Java 中有效地比较它们?
- (a) keep them as
List
s, sort them and compare them. (Comparable
) - (b) keep them as
Set
s and compare thehashCode
of the Sets?
- (a) 将它们保留为
List
s,对它们进行排序并进行比较。(Comparable
) - (b) 将它们保留为
Set
s 并比较hashCode
集合的 ?
background:
背景:
many comparisons need to be done Sets are small (usually < 5 elements per set).
需要做很多比较 集合很小(通常每个集合 < 5 个元素)。
回答by assylias
The proper way to compare two sets is to use the equals
method. I would not worry about performance unless you have proven that this is a part of your code that is causing performance issue (which I doubt). And considering the size of your sets (5 elements) this will be very fast (probably sub millisecond).
比较两组正确的方法是使用的equals
方法。我不会担心性能,除非您已经证明这是导致性能问题的代码的一部分(我对此表示怀疑)。考虑到您的集合(5 个元素)的大小,这将非常快(可能是亚毫秒)。
keep them as lists, sort them and compare them. (comparable)
将它们保留为列表,对它们进行排序并进行比较。(可比)
will certainly be slower as you will need to copy the elements, sort them and compare.
肯定会变慢,因为您需要复制元素,对它们进行排序和比较。
keep them as sets and compare the hashcode of the sets?
将它们保留为集合并比较集合的哈希码?
if 2 sets are equal (have the same content) they will have the same hashcode. The reciprocal is not true: 2 sets with different content may have the same hashcode. Also note that for a HashSet
for example, the hashcode is calculated by iterating over all the elements so it is not a free operation.
如果 2 个集合相等(具有相同的内容),它们将具有相同的哈希码。倒数是不正确的:具有不同内容的 2 个集合可能具有相同的哈希码。另请注意,HashSet
例如,哈希码是通过迭代所有元素来计算的,因此它不是免费操作。
回答by leonbloy
What's wrong with equals?
The docs states that it returns true if both are of same size and if containsAll()
returns true, sounds pretty efficient to me.
equals 有什么问题 ?文档指出,如果两者的大小相同,则containsAll()
返回 true,如果返回 true,对我来说听起来非常有效。
In any case, you should nevercompare the hashcode to test for equality, two different objects might have the same hashcode.
在任何情况下,你应该永远的哈希码进行比较测试平等,两个不同的对象可能具有相同的哈希码。
Update:As noted in the comments (and in assylias' answer) the hashcode can be used as part of the equality test logic (different hashcodes imply different objects - but not the reverse). My remark above means that the hashcode alone is not (in general) enough.
更新:如评论(以及 assylias 的回答)中所述,哈希码可以用作相等性测试逻辑的一部分(不同的哈希码意味着不同的对象 - 但不是相反)。我上面的评论意味着单独的哈希码(通常)是不够的。
回答by user1447445
Assuming you want to do a comparison whether set1
has exactly the sameelements of set2
.
假设你想要做一个比较是否set1
具有完全相同的元素set2
。
set1.equals(set2)
and also set2.equals(set1)
as to make sure both are exactly the same.
set1.equals(set2)
并set2.equals(set1)
确保两者完全相同。
回答by Marko Topolnik
If you have two HashSet
s, comparing them by Set.equals
will be O(n) because only one set needs to be iterated through, and the other will be checked by contains
, which is itself O(1).
如果你有两个HashSet
s,比较它们Set.equals
将是 O(n) 因为只有一个集合需要迭代,另一个将被检查contains
,它本身就是 O(1)。
Note that for sets as small as yours the difference between O(n) and O(n2) is neglible, so even the na?vest approaches will yield good performance.
请注意,对于像您这样小的集合,O(n) 和 O(n 2)之间的差异可以忽略不计,因此即使是最简单的方法也会产生良好的性能。