Java HashMap and TreeMap Explained

📘 Java 👁 58 views 📅 Dec 01, 2025
⏱ Estimated reading time: 2 min

Java provides Map implementations to store key-value pairs, where keys are unique and values can be duplicated. Two widely used Map implementations are HashMap and TreeMap.


1. HashMap

Definition

  • HashMap is a hash table-based implementation of the Map interface.

  • Stores data in key-value pairs.

  • Keys must be unique, values can be duplicated.

  • Allows one null key and multiple null values.

Features

  • Does not maintain insertion order.

  • Provides fast access and insertion (O(1) average time).

  • Not synchronized → not thread-safe.

Example:

import java.util.HashMap; public class Test { public static void main(String[] args) { HashMap map = new HashMap<>(); map.put(101, "Java"); map.put(102, "Python"); map.put(103, "C++"); System.out.println("HashMap: " + map); System.out.println("Value for key 102: " + map.get(102)); map.remove(103); System.out.println("After removal: " + map); } }

Output:

HashMap: {101=Java, 102=Python, 103=C++} Value for key 102: Python After removal: {101=Java, 102=Python}

2. TreeMap

Definition

  • TreeMap is a Red-Black tree-based implementation of the Map interface.

  • Stores key-value pairs in sorted order of keys (natural order or custom comparator).

  • Keys must be unique, values can be duplicated.

Features

  • Maintains ascending key order.

  • Slower than HashMap for insertion and access (O(log n)).

  • Does not allow null keys (but allows null values).

Example:

import java.util.TreeMap; public class Test { public static void main(String[] args) { TreeMap map = new TreeMap<>(); map.put(101, "Java"); map.put(103, "C++"); map.put(102, "Python"); System.out.println("TreeMap: " + map); System.out.println("First Key: " + map.firstKey()); System.out.println("Last Key: " + map.lastKey()); map.remove(103); System.out.println("After removal: " + map); } }

Output:

TreeMap: {101=Java, 102=Python, 103=C++} First Key: 101 Last Key: 103 After removal: {101=Java, 102=Python}

3. Comparison: HashMap vs TreeMap

FeatureHashMapTreeMap
OrderNo guaranteed orderKeys sorted in ascending order
ImplementationHash tableRed-Black tree
PerformanceFaster (O(1) avg)Slower (O(log n))
Null KeyAllows one null keyNot allowed
Null ValueAllows null valuesAllows null values
Use CaseFast lookup and insertionsSorted key-value pairs

4. Key Points

  • Both HashMap and TreeMap implement the Map interface.

  • HashMap is preferred for fast access.

  • TreeMap is preferred when sorted keys are required.

  • Both allow duplicate values but unique keys.


5. Conclusion

HashMap and TreeMap are powerful Map implementations in Java. Choosing the right Map depends on requirement: use HashMap for fast access and TreeMap for sorted key-value storage. Understanding their differences ensures efficient and reliable Java applications.


🔒 Some advanced sections are available for Registered Members
Register Now

Share this Post


← Back to Tutorials

Popular Competitive Exam Quizzes