The HashMap in the JDK is one of the most commonly used hash table-based map implementations. With a bit of a feature:
- Both key and value are allowed to be null.
- Non-thread safe.
- Default capacity length:1 << 4 = 16
- Default maximum capacity 1 << 30
- Default Factor loadfactor:0.75f
HashMap Idea of realization:
- Creates an array of the specified capacity to store linked list objects
- The key to the content is stored by the hash algorithm to obtain the corresponding hash value
- Length-1 the hash value with the array length to get the key in the array subscript
- If the location does not have a linked list object, the new linked list is inserted, and NULL is returned
- If a linked list object exists at that location and no specified key exists, the content is inserted at the end of the list, returning NULL
- If a linked list object exists at that location and the specified key exists, the content is returned when the contents are replaced
- size++
- When size >= threshold and the position of the key is not NULL, the capacity is expanded to 2 * length * loadfactor
HashMap Source Code Analysis