Java Map Hierarchy

Mahock AbrahamMahock Abraham
3 min read

πŸ—ΊοΈ Java Map Hierarchy – HashMap, TreeMap, LinkedHashMap Explained

✨ Introduction

In Java, the Map interface is part of the Java Collections Framework, but unlike List, Set, and Queue, it doesn’t extend the Collection interface.

The Map stores key-value pairs, where each key is unique, and each value is associated with a key. It’s one of the most commonly used structures in real-world projects and interviews.


πŸ”§ Map Interface Hierarchy

Here’s a simple overview of the core Map implementations:

Map
 β”œβ”€β”€ HashMap
 β”œβ”€β”€ LinkedHashMap
 β”œβ”€β”€ TreeMap
 └── Hashtable

Each implementation offers different performance, ordering, and null-handling characteristics.


🧱 Key Implementations of Map

1️⃣ HashMap

  • Unordered β€” no guarantee on the iteration order

  • Allows null keys and multiple null values

  • Non-synchronized

  • Backed by a hash table

πŸ’‘ When to Use:

When you need fast access, and ordering doesn’t matter.

Map<String, String> map = new HashMap<>();
map.put("A", "Apple");
map.put("B", "Banana");

βš™οΈ Big O:

OperationTime Complexity
put/get/removeO(1) average, O(n) worst
containsKeyO(1) average

2️⃣ LinkedHashMap

  • Maintains insertion order

  • Slightly slower than HashMap

  • Still allows null keys and values

πŸ’‘ When to Use:

When you want fast lookups and predictable iteration order.

Map<String, String> map = new LinkedHashMap<>();
map.put("B", "Banana");
map.put("A", "Apple");

3️⃣ TreeMap

  • Sorted based on the key (natural order or comparator)

  • No null keys allowed (but null values are allowed)

  • Backed by a Red-Black tree

πŸ’‘ When to Use:

When you need a sorted map or range queries.

Map<Integer, String> map = new TreeMap<>();
map.put(3, "Three");
map.put(1, "One");

βš™οΈ Big O:

OperationTime Complexity
put/get/removeO(log n)

4️⃣ Hashtable

  • Legacy class (before Java Collections Framework)

  • Thread-safe, but slower

  • No null keys or values allowed

⚠️ Recommendation:

Avoid in new code; use ConcurrentHashMap if you need thread safety.


πŸ”„ Map vs Collection

FeatureMapCollection
Key-Value Pairβœ… Yes❌ No
Duplicates❌ Keys must be uniqueβœ… Duplicates allowed (List)
Extends Collection?❌ Noβœ… Yes (List, Set, etc.)

πŸ” Synchronized Variants

  • For thread-safe operations:

      Map<String, String> syncMap = Collections.synchronizedMap(new HashMap<>());
    
  • Or use ConcurrentHashMap for better performance in multi-threaded environments.


βœ… Summary

Map TypeOrderingNull KeysThread SafeSorted
HashMapNoYesNoNo
LinkedHashMapInsertion orderYesNoNo
TreeMapSorted orderNoNoYes
HashtableNoNoYesNo

πŸ“Œ Final Thoughts

Understanding the Map hierarchy is crucial for writing efficient and readable Java code. Each implementation serves a unique use case:

  • πŸ” Use HashMap for general-purpose lookups

  • πŸ”„ Use LinkedHashMap when order matters

  • πŸ”’ Use TreeMap when sorted keys are required

  • 🧡 Use ConcurrentHashMap for thread-safe performance


0
Subscribe to my newsletter

Read articles from Mahock Abraham directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Mahock Abraham
Mahock Abraham