HashMap source code analysis, hashmap source code
In JDK, HashMap is the most common Map implementation based on hash tables. Has the following features:
- Both key and value are allowed to be null.
- Non-thread security.
- Default capacity length: 1 <4 = 16
- Default maximum capacity: 1 <30
- Default factor: loadfactor: 0.75f
Implementation of HashMap:
- Create an array of the specified capacity to store linked list objects
- Use the hash algorithm to obtain the corresponding hash value for the key of the stored content
- Take the hash value and length-1 of the array and obtain the subscript of the key in the array.
- If no linked list object exists at this position, if a new linked list is inserted, null is returned.
- If a linked list object does not exist at this position and the specified key does not exist, if the content is inserted to the end of the linked list, null is returned.
- If a linked list object already exists at this position and a specified key exists, replace the content and return the content.
- Size ++
- When size> = threshold and the location of the key is not null, scale up. The size is 2 * length * loadfactor.