Java Android Pair'上是否有任何参考源;s哈希代码实现,它使用XOR运算符
当我看到Android Pair的“旧”版本的hashCode时,它的实现如下所示: Josh Bloch的有效Java- 然而,当我看到“最新”版本的Android Pair的Java Android Pair'上是否有任何参考源;s哈希代码实现,它使用XOR运算符,java,android,Java,Android,当我看到Android Pair的“旧”版本的hashCode时,它的实现如下所示: Josh Bloch的有效Java- 然而,当我看到“最新”版本的Android Pair的hashCode,它看起来是这样的 我可以知道,关于为什么要使用XOR运算符,有没有参考资料?我认为我们不应该违反Josh Bloch的教导:)?不清楚作者为什么要从素数技术转向XOR技术,但这种改变的动机是需要在对类中支持null值。请参阅此提交:。理论上,如果两个对象都不是null,那么实现可以使用旧版本,但如果其中
hashCode
,它看起来是这样的
我可以知道,关于为什么要使用
XOR
运算符,有没有参考资料?我认为我们不应该违反Josh Bloch的教导:)?不清楚作者为什么要从素数技术转向XOR技术,但这种改变的动机是需要在对类中支持null
值。请参阅此提交:。理论上,如果两个对象都不是null
,那么实现可以使用旧版本,但如果其中一个是,那么hashCode将成为该对的非null组件的hashCode(或者如果两者都为null,则为0
)
/**
* 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;
}
/**
* 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());
}