简体   繁体   中英

Load factor and capacity of Hash Table

当散列表中的条目数超过负载因子和当前容量的乘积时,容量如何增加?

It depends on the underlying implementation. For example in the HashMap the underlying storage is an array:

transient Entry[] table;

The Entry object contains the key and the value. When the capacity is not enough (as you said correctly, exceeds the product of the load factor and the current capacity ), a new array is created and old values are copied into it.

See the sourcecode of HashMap for OpenJdk 7 and look for void resize(int newCapacity) . The most important lines in the method are:

Entry[] newTable = new Entry[newCapacity];   //create the new table
transfer(newTable);                          //transfer and rehash the data
table = newTable;                            //from now on use the new table
threshold = (int)(newCapacity * loadFactor); //compute the new threshold

threshold is the maximum number of elements that can be contained before increasing the size again. transfer() also rehashes the elements, so elements will be probably stored in different array indexes, compared to their original position. You can look at the code, is surprisingly simple to read.

The load factor is a measure of how full the hash table is allowed to get before its capacity is automatically increased. When the number of entries in the hashtable exceeds the product of the load factor and the current capacity, the capacity is increased by calling the rehash method.

here is a glimpse of rehash method

 int oldCapacity = table.length;
Entry[] oldMap = table;

int newCapacity = oldCapacity * 2 + 1;
Entry[] newMap = new Entry[newCapacity];

As you can see its actually doubling the capacity of Entry[] once the product of load factor and current capacity increases threshold.

see the rehash method of hashtable for more details.

Rehash Method

When you create a new hashtable it is created with a default size and load factor.

Source code

public Hashtable() {
        this(11, 0.75f);
}

When you add elements into it

it checks for the

if (count >= threshold) {

}

respectively creates a new array and copies as Stivlo said.

private transient Entry[] table;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM