What is Load Factor in HashMap?

The Load Factor in a HashMap defines how full the HashMap can get before its capacity is automatically increased (resized).




In simple terms:

👉 Load Factor controls when HashMap should grow to maintain good performance.


🔹 Default Load Factor

The default load factor value in Java HashMap is:

0.75 (75%)

This means when the HashMap becomes 75% full, it will resize automatically.


🔹 How It Works

Formula:

Threshold = Capacity × Load Factor

Example:

  • Initial Capacity = 16

  • Load Factor = 0.75

Threshold = 16 × 0.75 = 12

👉 When 12 entries are inserted, HashMap resizes (capacity becomes 32).


🔹 Why Load Factor is Important?

It balances between:

Default value 0.75 provides the best balance between memory and performance.


🔹 Resizing Process

When threshold is reached:

  1. HashMap capacity doubles

  2. All elements are rehashed

  3. New bucket indexes are calculated

This process is called Rehashing.


🔹 Custom Load Factor Example

HashMap<Integer, String> map =
        new HashMap<>(32, 0.5f);

Here:

  • Initial capacity = 32

  • Load factor = 0.5 (resizes earlier)


🚀 Promotional Content

Understand HashMap internals, collections framework, and real-world Java concepts through No 1 Java Real Time Projects Online Training in ammerpet.

Comments

Popular posts from this blog

How Does HashMap Work Internally in Java?

What is Docker Used for in Java Applications?

Java Future Interface: Complete Practical Guide with Real-Time Examples for Modern Developers (2026)