Java.Collections.What is the initial number of buckets in HashMap?

In Java, the initial number of buckets in a HashMap depends on the constructor used when creating the map.

By default:

Map<K, V> map = new HashMap<>();

👉 The initial capacity is 16 buckets.

Key details:

  • Initial Capacity: 16 (this means 16 buckets internally).
  • Load Factor: 0.75 (this means the map resizes when 75% of the capacity is filled, i.e., 12 entries in this case).
  • Buckets: These are actually positions in an internal array (Node[] table) where hash collisions are resolved using linked lists or balanced trees (since Java 8).

You can also specify a custom capacity:

Map<K, V> map = new HashMap<>(32); // 32 buckets initially
This entry was posted in Без рубрики. Bookmark the permalink.