为什么哈希函数对哈希码做了XOR?

时间:2015-02-27 07:14:13

标签: java hash hashmap hashcode

我阅读了解释但是我无法通过对hashCode进行XOR来理解我们正在实现的目标。任何人都可以举一些例子。

static final int hash(Object key) {
    int h;
    return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
}

此代码取自HashMap源代码。我只是想知道他们为什么使用XOR,Marko回答说HashMap实现正确使用低端位。我认为不仅HashMap,其他集合也会做同样的事情,这就是为什么我没有提到任何集合名称。我不明白为什么人们"降价"这个问题。

2 个答案:

答案 0 :(得分:2)

这是防止“坏”哈希码的典型策略:这样的低端比特不够可变。 Java的HashMap实现仅依赖于哈希码的低端位来选择存储桶。

然而,这段代码的动机很久以前就已经过期了,因为HashMap已经有了自己的位传播。如果在Hashtable上使用它会有意义,但当然自2000年以来编写的代码都不应该使用它。

答案 1 :(得分:0)

代码是openjdk java HashMap源代码:HashMap.java

     /**
     * Computes key.hashCode() and spreads (XORs) higher bits of hash
     * to lower.  Because the table uses power-of-two masking, sets of
     * hashes that vary only in bits above the current mask will
     * always collide. (Among known examples are sets of Float keys
     * holding consecutive whole numbers in small tables.)  So we
     * apply a transform that spreads the impact of higher bits
     * downward. There is a tradeoff between speed, utility, and
     * quality of bit-spreading. Because many common sets of hashes
     * are already reasonably distributed (so don't benefit from
     * spreading), and because we use trees to handle large sets of
     * collisions in bins, we just XOR some shifted bits in the
     * cheapest possible way to reduce systematic lossage, as well as
     * to incorporate impact of the highest bits that would otherwise
     * never be used in index calculations because of table bounds.
     */
    static final int hash(Object key) {
        int h;
        return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
    }

异或是为了使散列结果更加分散