Sobes.tech
Junior — Middle

What is the default number of buckets used in the HashMap implementation in Java?

sobes.tech AI

Answer from AI

By default, the HashMap implementation in Java uses 16 buckets. This means that when creating a new HashMap object without specifying an initial capacity, the internal array of buckets has a size of 16.

Each bucket is a linked list or a tree (starting from Java 8, when there are many collisions), which stores key-value pairs with the same hash modulo the array size.

Example of creating a HashMap with the default size:

HashMap<String, Integer> map = new HashMap<>();
// The internal array of buckets has a size of 16

This value is chosen as a power of two to optimize the calculation of the bucket index using bitwise operations.

What is the default number of buckets used in the… - sobes.tech