Guava computes the hash code of a set like this:
static int hashCodeImpl(Set<?> s) {
int hashCode = 0;
for(Iterator var2 = s.iterator(); var2.hasNext(); hashCode = ~(~hashCode)) {
Object o = var2.next();
hashCode += o != null ? o.hashCode() : 0;
}
return hashCode;
}
this is efficient and elegant - we use a commutative operation (addition) to "mix in" the hash codes of the objects, so that we get the same value regardless of order we iterate over the elements.
What I'm not sure about is the update condition in the for loop: hashCode = ~(~hashCode) seems to have no effect (IntelliJ's code inspector suggests to simplify the expression and then remove the self-assignment).
What's going on?