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

查看:170
本文介绍了为什么每次添加新元素时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.

推荐答案

ArrayListhashCodeArrayList中存储的所有元素的hashCode s的函数,因此它不是当容量改变时,它也会改变,只要您添加或删除一个元素或以改变其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天全站免登陆