为什么每次添加新元素时 ArrayList 的 hashCode() 都会改变? [英] Why does the hashCode() of an ArrayList change every time you add a new element?

查看:40
本文介绍了为什么每次添加新元素时 ArrayList 的 hashCode() 都会改变?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据我对ArrayList的理解,默认容量是10,当它增长到超过10时,它会创建一个具有新容量的新对象等等..

As per my understanding of ArrayList, the default capacity is 10 and when it grows beyond 10, it will create a new object with new capacity and so on..

出于好奇,我输入了以下程序来检查 hashcode()ArrayList 对象:

So out of curiosity, I typed following program to check hashcode() for ArrayList object:

public class TestCoreJava {

    public static void main(String [] args){

        ArrayList al = new ArrayList();

        for(int i=0;i<15;i++){

            al.add("Temp"+i);
            System.out.println("Hashcode for "+i+" element "+al.hashCode());
        }
    }
}

根据上述情况,当我没有为 ArrayList 设置初始容量时,默认值为 10.因此,在添加第 11 个元素时,它将创建一个新对象并增加 ArrayList 的容量.

According to above scenario, when I am not setting Initial capacity for ArrayList the default would be 10. So while adding 11th element, it will create a new object and increase the capacity for ArrayList.

当我打印 ArrayList 对象的哈希码时,它每次都会给出一个新的 hashcode().

When I print the hashcode for ArrayList object, it is giving a new hashcode() each time.

以下是o/p:

Hashcode for 0 element 80692955
Hashcode for 1 element -1712792766
Hashcode for 2 element -1476275268
Hashcode for 3 element 1560799875
Hashcode for 4 element 1220848797
Hashcode for 5 element -727700028
Hashcode for 6 element -1003171458
Hashcode for 7 element -952851195
Hashcode for 8 element 607076959
Hashcode for 9 element 1720209478
Hashcode for 10 element -6600307
Hashcode for 11 element -1998096089
Hashcode for 12 element 690044110
Hashcode for 13 element -1876955640
Hashcode for 14 element 150430735

根据默认容量的概念,直到第 10 个元素它应该打印相同的 hashcode(),因为在此之前不需要创建新对象,但事实并非如此.>

According to the concept of default capacity, till 10th element it should have printed same hashcode() as no new object needs to be created until that point, but it is not the case.

推荐答案

ArrayListhashCodehashCode的函数ArrayList 中存储的所有元素,因此它不会在容量变化时发生变化,只要您添加或删除元素或以更改其 hashCode 的方式改变其中一个元素,它就会发生变化.

The hashCode of ArrayList is a function of the hashCodes of all the elements stored in the ArrayList, so it doesn't change when the capacity changes, it changes whenever you add or remove an element or mutate one of the elements in a way that changes its hashCode.

这是 Java 8 的实现(它实际上是在 AbstractList 中实现的):

Here's the Java 8 implementation (it's actually implemented in AbstractList) :

public int hashCode() {
    int hashCode = 1;
    for (E e : this)
        hashCode = 31*hashCode + (e==null ? 0 : e.hashCode());
    return hashCode;
}

顺便说一句,这是出现在List接口的hashCode()的Javadoc中的确切代码:

BTW, this is the exact code that appears in the Javadoc of hashCode() of the List interface :

int java.util.List.hashCode()

返回此列表的哈希码值.列表的哈希码定义为以下计算的结果:

Returns the hash code value for this list. The hash code of a list is defined to be the result of the following calculation:

int hashCode = 1;
for (E e : list)
    hashCode = 31*hashCode + (e==null ? 0 : e.hashCode());

这篇关于为什么每次添加新元素时 ArrayList 的 hashCode() 都会改变?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆