JavaAdvanced#collections#hashmap

How does HashMap work internally?

Buckets are an array of Nodes. The key's hashCode is hashed and modded by capacity to pick a bucket. Collisions chain via linked list; when a bucket exceeds 8 entries (Java 8+), it converts to a red-black tree. Resizes when load factor (0.75) is exceeded.

Example
map.put("key", 1);
// int hash = key.hashCode(); bucket = hash & (capacity-1);
// if two keys land in same bucket, they form a linked list (or tree if >8 entries)

Related Questions

1
JavaAdvanced#collections

What is the difference between fail-fast and fail-safe iterators?

Open
2
JavaIntermediate#java8#streams

Explain Java 8 Streams.

Open
3
JavaIntermediate#java8#lambda

What are lambda expressions?

Open