Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does Guava Set difference work?

Tags:

java

guava

when are two objects considered to be different in Sets.difference.When they have a different hash code or when object.equals returns a false.

like image 671
programer8 Avatar asked Dec 26 '22 16:12

programer8


1 Answers

The javadoc says:

The returned set contains all elements that are contained by set1 and not contained by set2

It thus means that the rule depends on the type of the two sets. If the Set is a HashSet, for example, equals() will be used. If the set is a TreeSet, compareTo() (or the comparator's compare() method) will be used. If an IdentityHashSet is used, the identity of the object will be used.

hashCode() will never be used by any (correct) Set implementation to determine equality, because two unequal objects may have the same hashCode.

like image 165
JB Nizet Avatar answered Dec 28 '22 05:12

JB Nizet