Read Java HashMap source

A sloppy hash table should be understood at the implementation level, so make a note of what you noticed when you read it briefly. I couldn't read it 10 years ago, but this time I could read it quickly. (I read Adopt Open JDK 13)

Inside the table, data was managed by the following classes. In short, it is implemented by Closed Addressing.

static class Node<K,V> implements Map.Entry<K,V> {
    final int hash;
    final K key;
    V value;
    Node<K,V> next;

/ ** Middle clothes * / }

As an aside, the Unified Map of Eclipse Collection (10.1) was also Open Addressing, but this was chained in an array instead of chaining with pointers. It seems that the reason is that you want to use the memory cache like Open Addressing.

Hash value calculation. When the number of elements is small, in order to avoid the storage location being determined only by the lower bits, the upper 16 bits are XORed to the lower 16 bits so that the upper bits can also be used.

static final int hash(Object key) {
    int h;
    return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
}

How to look up the hash table. The index was the value obtained by subtracting 1 from the number of elements n in the table and ANDing.

if ((p = tab[i = (n - 1) & hash]) == null)
    tab[i] = newNode(hash, key, value, null);

By the way, the number of elements in the table (tab.length above) seems to be a multiple of 2, and when -1 is set from that, all bits are set, so it seems that the operation leaves the lower bits.

Recommended Posts

Read Java HashMap source
Java HashMap class
Read JSON in Java
Comments in Java source
java (split source file)
Eval Java source from Java
Initializing HashMap in Java
Read binary files in Java 1
Read standard input in Java
Read binary files in Java 2
[Java] Tips for writing source
[Java] [Android] Read ini file
Java beginners read Hello World
Java HashMap, entrySet [Personal memo]
Easily read text files in Java (Java 11 & Java 7)
[Java] Difference between Hashmap and HashTable
I read the source of ArrayList I read
Read CSV in Java (Super CSV Annotation)
I read the source of Integer
I read the source of Long
Java
Java source code reading java.lang.Math class
I read the source of Short
Java
I read the source of Byte
Java HashMap Transform js json Formal
I read the source of String
Basic structure of Java source code
Java for All! I read everyone's Java #minjava
[Java] How to use the HashMap class
[JAWS-UG CLI] CodeBuild: # 1 Creating Source Code (Java)
[Read Effective Java] Chapter 2 Item 7 "Avoid Finalizers"
Read Felica using RC-S380 (PaSoRi) in Java
Read xlsx file in Java with Selenium
[Java] Flow from source code to execution
HashMap # putAll () behaves differently between Java 7 and Java 8