为什么Java实现Set和ArrayList的不同哈希码方法?

Hon*_*hen 4 java hashcode

 // this is the hashCode method of Set
public int hashCode() {
    int h = 0;
    Iterator<E> i = iterator();
    while (i.hasNext()) {
        E obj = i.next();
        if (obj != null)
            h += obj.hashCode();
    }
    return h;
}



//this is the hashCode method of List
public int hashCode() {
    int hashCode = 1;
    for (E e : this)
        hashCode = 31*hashCode + (e==null ? 0 : e.hashCode());
    return hashCode;
}
Run Code Online (Sandbox Code Playgroud)

为什么Java使用这两种不同的方法?有什么与Set and List的特征有关的东西吗?为什么使用31但不使用其他数字?谢谢!

ysh*_*vit 7

集是无序的,因此{a, b, c}必须具有与相同的哈希码{c, b, a}。加法是可交换的,因此添加元素的hashCodes可以为您提供该属性。

列表是有序的,因此虽然[a, b, c] 可能具有与相同的哈希码[c, b, a],但它不需要-而且最好不要这样做,因为尽可能多的不相等对象应尝试具有不相等的hashCodes 。ArrayList.hashCode实现具有该属性。

请注意,Set和List都定义了实现必须如何定义equalshashCodeSet.hashCodeList.hashCode),因此这些相应集合的任何(兼容)实现看起来都差不多。这为您提供了有用的属性,即与基础Set无关,包含相同元素的Set与其他Set相等(因此具有相同的hashCode)。