When I look at "old" version of Android Pair's hashCode
, its implementation is following text in
Josh Bloch's Effective Java - Best implementation for hashCode method
"Old" Pair.java
/**
* Compute a hash code using the hash codes of the underlying objects
* @return a hashcode of the Pair
*/
public int hashCode() {
int result = 17;
result = 31 * result + first.hashCode();
result = 31 * result + second.hashCode();
return result;
}
However, when I look at "latest" version of Android Pair's hashCode
, it looks like this
"New" Pair.java
/**
* Compute a hash code using the hash codes of the underlying objects
*
* @return a hashcode of the Pair
*/
@Override
public int hashCode() {
return (first == null ? 0 : first.hashCode()) ^ (second == null ? 0 : second.hashCode());
}
May I know, is there any reference source, on why XOR
operator is being used? I thought we shouldn't violate Josh Bloch's teaching :) ?