How to efficiently compare Sets? [duplicate]

2019-02-24 17:31发布

This question already has an answer here:

Given two Sets: how to compare them efficiently in Java?

  • (a) keep them as Lists, sort them and compare them. (Comparable)
  • (b) keep them as Sets and compare the hashCode of the Sets?

background:

many comparisons need to be done Sets are small (usually < 5 elements per set).

4条回答
疯言疯语
2楼-- · 2019-02-24 18:08

Assuming you want to do a comparison whether set1 has exactly the same elements of set2.

set1.equals(set2) and also set2.equals(set1) as to make sure both are exactly the same.

查看更多
Bombasti
3楼-- · 2019-02-24 18:23

If you have two HashSets, 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).

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.

查看更多
看我几分像从前
4楼-- · 2019-02-24 18:26

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).

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.

查看更多
迷人小祖宗
5楼-- · 2019-02-24 18:30

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.

In any case, you should never compare 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.

查看更多
登录 后发表回答