</>

Technology

Core Java

Difficulty

Intermediate

Interview Question

What is the difference between HashMap, LinkedHashMap, and TreeMap in Java?

Answer

HashMap vs LinkedHashMap vs TreeMap

All implement the Map interface but differ in ordering and performance.

HashMap — No Order Guaranteed

Java
Map<String, Integer> hashMap = new HashMap<>();
hashMap.put("Banana", 2);
hashMap.put("Apple", 1);
hashMap.put("Cherry", 3);
hashMap.put("Date", 4);

// Iteration order is NOT predictable
hashMap.forEach((k, v) -> System.out.println(k + "=" + v));
// Could print in any order: Cherry=3, Date=4, Apple=1, Banana=2

Performance: O(1) average for get/put.

LinkedHashMap — Insertion Order

Java
Map<String, Integer> linkedMap = new LinkedHashMap<>();
linkedMap.put("Banana", 2);
linkedMap.put("Apple", 1);
linkedMap.put("Cherry", 3);
linkedMap.put("Date", 4);

// Always prints in insertion order
linkedMap.forEach((k, v) -> System.out.println(k + "=" + v));
// Banana=2, Apple=1, Cherry=3, Date=4

Also supports access-order (LRU cache):

Java
// true = access-order (most-recently-accessed last)
Map<String, Integer> lruCache = new LinkedHashMap<>(16, 0.75f, true) {
    @Override
    protected boolean removeEldestEntry(Map.Entry<String, Integer> eldest) {
        return size() > 3;  // max 3 entries — evict oldest
    }
};

TreeMap — Sorted by Key

Java
Map<String, Integer> treeMap = new TreeMap<>();
treeMap.put("Banana", 2);
treeMap.put("Apple", 1);
treeMap.put("Cherry", 3);
treeMap.put("Date", 4);

// Always prints in natural (alphabetical) key order
treeMap.forEach((k, v) -> System.out.println(k + "=" + v));
// Apple=1, Banana=2, Cherry=3, Date=4

// Extra methods
System.out.println(treeMap.firstKey()); // "Apple"
System.out.println(treeMap.lastKey());  // "Date"
System.out.println(treeMap.headMap("Cherry")); // {Apple=1, Banana=2}
System.out.println(treeMap.tailMap("Cherry")); // {Cherry=3, Date=4}

Custom key order:

Java
Map<String, Integer> reverseMap = new TreeMap<>(Comparator.reverseOrder());
reverseMap.put("Banana", 2);
reverseMap.put("Apple", 1);
// Date=4, Cherry=3, Banana=2, Apple=1

Comparison Table

FeatureHashMapLinkedHashMapTreeMap
OrderNoneInsertion orderSorted (key)
Null key✅ 1 null✅ 1 null❌ No null key
PerformanceO(1)O(1)O(log n)
Thread-safe
ImplementsMapMapNavigableMap
Use whenJust key-valueNeed insertion orderNeed sorted keys

In Automation Testing

Java
// HashMap — store test data (order doesn't matter)
Map<String, String> testData = new HashMap<>();
testData.put("username", "alice");
testData.put("password", "pass123");

// LinkedHashMap — maintain form field order for ordered input
Map<String, String> formData = new LinkedHashMap<>();
formData.put("firstName", "John");
formData.put("lastName", "Doe");
formData.put("email", "john@example.com");
// Fill form fields in order
formData.forEach((field, value) ->
    driver.findElement(By.name(field)).sendKeys(value));

// TreeMap — sorted test parameter names for reports
Map<String, String> sortedParams = new TreeMap<>(testParameters);
sortedParams.forEach((k, v) -> report.addParameter(k, v));

Follow AutomateQA

Related Topics