Java HashMap, LinkedHashMap, and TreeMap – Complete Guide with Examples


Learn Java Map implementations—HashMap, LinkedHashMap, and TreeMap—their differences, features, and how to store and manage key-value pairs efficiently in Java applications.

HashMap, LinkedHashMap, and TreeMap in Java – Complete Detailed Tutorial

The Map interface in Java stores key-value pairs, and its commonly used implementations are HashMap, LinkedHashMap, and TreeMap. Each has different ordering and performance characteristics.

1. HashMap

  1. Implements Map interface
  2. Stores key-value pairs
  3. Keys are unique, values can be duplicated
  4. No guaranteed order
  5. Allows one null key and multiple null values
  6. Backed by hash table, provides fast access

Common Methods:

MethodDescription
put(K key, V value)Adds a key-value pair
get(Object key)Retrieves value for a key
remove(Object key)Removes key-value pair
containsKey(Object key)Checks if key exists
containsValue(Object value)Checks if value exists
size()Returns number of entries

Example – Using HashMap


import java.util.HashMap;
import java.util.Map;

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

System.out.println("HashMap: " + map);
System.out.println("Value for key 2: " + map.get(2));
map.remove(3);
System.out.println("After removal: " + map);
}
}

Output (order may vary):


HashMap: {1=Apple, 2=Grapes, 3=Orange}
Value for key 2: Grapes
After removal: {1=Apple, 2=Grapes}

2. LinkedHashMap

  1. Extends HashMap, implements Map interface
  2. Maintains insertion order
  3. Allows one null key and multiple null values
  4. Slightly slower than HashMap due to ordering maintenance

Example – Using LinkedHashMap


import java.util.LinkedHashMap;
import java.util.Map;

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

System.out.println("LinkedHashMap: " + map);
map.remove(2);
System.out.println("After removal: " + map);
}
}

Output:


LinkedHashMap: {1=Apple, 2=Banana, 3=Orange}
After removal: {1=Apple, 3=Orange}

Explanation:

  1. Preserves insertion order
  2. Useful when order matters

3. TreeMap

  1. Implements SortedMap interface
  2. Stores key-value pairs in ascending sorted order by key
  3. Does not allow null keys, allows multiple null values
  4. Backed by Red-Black tree
  5. Slower than HashMap and LinkedHashMap due to sorting overhead

Example – Using TreeMap


import java.util.Map;
import java.util.TreeMap;

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

System.out.println("TreeMap: " + map);
System.out.println("First key: " + ((TreeMap<Integer, String>) map).firstKey());
System.out.println("Last key: " + ((TreeMap<Integer, String>) map).lastKey());
map.remove(2);
System.out.println("After removal: " + map);
}
}

Output:


TreeMap: {1=Apple, 2=Banana, 3=Orange}
First key: 1
Last key: 3
After removal: {1=Apple, 3=Orange}

4. HashMap vs LinkedHashMap vs TreeMap

FeatureHashMapLinkedHashMapTreeMap
OrderUnorderedInsertion orderSorted order (ascending)
Null KeyAllowed (1)Allowed (1)Not allowed
Null ValueAllowedAllowedAllowed
PerformanceFast (O(1))Slightly slowerSlower (O(log n))
ImplementationHash tableHash table + linked listRed-Black tree

5. Key Points

  1. Choose HashMap for fast access without order
  2. Choose LinkedHashMap to maintain insertion order
  3. Choose TreeMap to maintain sorted key order
  4. All implement Map interface, store key-value pairs, and provide methods like put, get, remove

6. Summary

  1. HashMap: fast, unordered, allows one null key
  2. LinkedHashMap: maintains insertion order
  3. TreeMap: sorted order by keys, no null keys
  4. Essential for efficient key-value storage in Java applications