Java-15.7 Map (6)-Introduction to the working principle of HashMap-load factor and Performance
This chapter introduces the working principle of HashMap by discussing the load factor and performance.
1. What is a load factor? What does it do?
The following code is the load factor.
/** * The load factor used when none specified in constructor. */ static final float DEFAULT_LOAD_FACTOR = 0.75f;
Purpose: control when the map needs to be resized through the resize method, and then copy the original elements to the new container using the rehash method.
2. Load Factor and Performance
/** * Adds a new entry with the specified key, value and hash code to * the specified bucket. It is the responsibility of this * method to resize the table if appropriate. * * Subclass overrides this to alter the behavior of put method. */ void addEntry(int hash, K key, V value, int bucketIndex) {Entry
e = table[bucketIndex]; table[bucketIndex] = new Entry
(hash, key, value, e); if (size++ >= threshold) resize(2 * table.length); }
Let's take a look at the add method above. It is mainly used for the put or putall method to put the elements in the linked list, but he must check the map size to determine whether to resize.
Let's take a look at the two constructors of hashmap below:
public HashMap() { this.loadFactor = DEFAULT_LOAD_FACTOR; threshold = (int)(DEFAULT_INITIAL_CAPACITY * DEFAULT_LOAD_FACTOR); table = new Entry[DEFAULT_INITIAL_CAPACITY]; init(); }
public HashMap(int initialCapacity, float loadFactor) { if (initialCapacity < 0) throw new IllegalArgumentException("Illegal initial capacity: " + initialCapacity); if (initialCapacity > MAXIMUM_CAPACITY) initialCapacity = MAXIMUM_CAPACITY; if (loadFactor <= 0 || Float.isNaN(loadFactor)) throw new IllegalArgumentException("Illegal load factor: " + loadFactor); // Find a power of 2 >= initialCapacity int capacity = 1; while (capacity < initialCapacity) capacity <<= 1; this.loadFactor = loadFactor; threshold = (int)(capacity * loadFactor); table = new Entry[capacity]; init(); }
Observe the preceding two functions and pay attention to the threshold Parameter. In the default constructor, threshold is determined by the load factor and initial capacity. However, the hashmap constructor is rewritten below, threshold is determined by two parameters, and we will look back at the resize method. Whether the execution of threshold is determined by threshold. Therefore, we can draw a conclusion:
Whether resize is determined by the load factor and initial capacity.
By observing the source code of resize, we can draw a conclusion that the more resize, the lower the performance.
Resize source code:
void resize(int newCapacity) { Entry[] oldTable = table; int oldCapacity = oldTable.length; if (oldCapacity == MAXIMUM_CAPACITY) { threshold = Integer.MAX_VALUE; return; } Entry[] newTable = new Entry[newCapacity]; transfer(newTable); table = newTable; threshold = (int)(newCapacity * loadFactor); }
Because resize needs to copy the old elements to the new container.
3. Performance Testing:
package com.ray.ch15;import java.util.HashMap;public class Test {public static void main(String[] args) {HashMap
map = new HashMap
();long startTime = System.currentTimeMillis();for (int i = 0; i < 100000; i++) {map.put(i, "a");}long endTime = System.currentTimeMillis();System.out.println(endTime - startTime);System.out.println("---------------");HashMap
map2 = new HashMap
(100000, 1f);startTime = System.currentTimeMillis();for (int i = 0; i < 100000; i++) {map2.put(i, "a");}endTime = System.currentTimeMillis();System.out.println(endTime - startTime);}}
Output:
47
---------------
15
The above test results can already explain the above conclusions.
Summary: This chapter introduces the working principle of HashMap by discussing the load factor and performance.