Introduction

In Java, the Map interface is widely used for storing key-value pairs. But when choosing a specific implementation—HashMap, LinkedHashMap, or TreeMap—it can be confusing because they all store data in a similar way. However, they differ in ordering, internal working, performance, and use cases. This article explains each map type in simple, natural language along with code examples so you can easily understand when to use each one.

1. HashMap in Java

HashMap is the most commonly used map implementation. It stores data in no particular order.

Key Features

Example

import java.util.HashMap;

public class Main {
    public static void main(String[] args) {
        HashMap<Integer, String> map = new HashMap<>();
        map.put(3, "Apple");
        map.put(1, "Mango");
        map.put(2, "Banana");

        System.out.println(map); // Output: Order is unpredictable
    }
}

When to Use HashMap

2. LinkedHashMap in Java

LinkedHashMap maintains the insertion order of elements. It works like HashMap but keeps a doubly-linked list internally.

Key Features

Example

import java.util.LinkedHashMap;

public class Main {
    public static void main(String[] args) {
        LinkedHashMap<Integer, String> map = new LinkedHashMap<>();
        map.put(3, "Apple");
        map.put(1, "Mango");
        map.put(2, "Banana");

        System.out.println(map); // Output: {3=Apple, 1=Mango, 2=Banana}
    }
}

When to Use LinkedHashMap

3. TreeMap in Java

TreeMap stores keys in sorted (ascending) order. It uses a Red-Black Tree internally.

Key Features

Example

import java.util.TreeMap;

public class Main {
    public static void main(String[] args) {
        TreeMap<Integer, String> map = new TreeMap<>();
        map.put(3, "Apple");
        map.put(1, "Mango");
        map.put(2, "Banana");

        System.out.println(map); // Output: {1=Mango, 2=Banana, 3=Apple}
    }
}

When to Use TreeMap

Key Differences Between HashMap, LinkedHashMap, and TreeMap

1. Ordering

2. Performance

OperationHashMapLinkedHashMapTreeMap
InsertO(1)O(1)O(log n)
SearchO(1)O(1)O(log n)
DeleteO(1)O(1)O(log n)

3. Null Support

4. Internal Working

Real-World Examples

When to Use HashMap

When to Use LinkedHashMap

When to Use TreeMap

Example: Comparing All Three

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Map<Integer, String> hashMap = new HashMap<>();
        Map<Integer, String> linkedHashMap = new LinkedHashMap<>();
        Map<Integer, String> treeMap = new TreeMap<>();

        int[] keys = {3, 1, 2};
        for (int key : keys) {
            hashMap.put(key, "Value" + key);
            linkedHashMap.put(key, "Value" + key);
            treeMap.put(key, "Value" + key);
        }

        System.out.println("HashMap: " + hashMap);
        System.out.println("LinkedHashMap: " + linkedHashMap);
        System.out.println("TreeMap: " + treeMap);
    }
}

Output

Conclusion

HashMap, LinkedHashMap, and TreeMap all implement the Map interface but behave very differently. HashMap offers the best performance without order. LinkedHashMap preserves insertion order, making it suitable for predictable iteration. TreeMap automatically sorts keys, making it ideal for ordered or range-based operations. By understanding these differences, you can choose the right map for your Java application and write efficient, readable, and optimized code.