Java.Collections.LinkedHashMap – what is from LinkedList and what is from HashMap?

🔍 What is LinkedHashMap?

LinkedHashMap is a hash table + doubly linked list implementation of the Map interface.
It maintains the insertion order (or access order) of entries, unlike regular HashMap.


🧩 Inherited from HashMap

FeatureDescription
🔑 Fast O(1) accessUses hash buckets for quick lookup
🧠 Uses .hashCode() + .equals()For hashing and key comparison
✅ Allows null key and valuesJust like HashMap
🔐 Not thread-safeSame as HashMap

So performance-wise, it works just like HashMap for lookup, put, remove — fast and efficient.


🧩 Added from LinkedList

FeatureDescription
🪝 Doubly linked listMaintains insertion or access order of entries
📜 Predictable iterationIterates in the order items were added or accessed
🔁 Custom order optionCan be set to access-order (LRU-style) if needed

Internally, LinkedHashMap adds a linked list of entries that tracks the order of insertion (or access).


👀 Constructor Example

LinkedHashMap<String, String> map = new LinkedHashMap<>(
    16, 0.75f, false // false = insertion order, true = access order
);

🚀 Real Use Cases

Use CaseWhy It Fits
Ordered cachingKeeps insertion or access order
Building LRU cacheWith accessOrder = true + removeEldestEntry() override
Exporting JSON/XMLKeys stay in the same order they were inserted
Pretty logging/debuggingEasier to read when order is preserved

🧠 TL;DR:

LinkedHashMap =
HashMap’s speed 🔥
LinkedList’s order tracking 📜
→ Perfect when you want fast lookup and predictable order.

Let’s look at a practical example of using LinkedHashMap and iterating over it to see how it preserves order — either insertion order or access order.


🔹 Example 1: Iteration in Insertion Order (default)

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

public class InsertionOrderExample {
    public static void main(String[] args) {
        Map<String, Integer> map = new LinkedHashMap<>();

        map.put("apple", 1);
        map.put("banana", 2);
        map.put("cherry", 3);

        System.out.println("Iteration in insertion order:");
        for (Map.Entry<String, Integer> entry : map.entrySet()) {
            System.out.println(entry.getKey() + " => " + entry.getValue());
        }
    }
}

🧾 Output:

Iteration in insertion order:
apple => 1
banana => 2
cherry => 3

✅ Order is exactly how we inserted the items.


🔸 Example 2: Iteration in Access Order

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

public class AccessOrderExample {
    public static void main(String[] args) {
        Map<String, Integer> map = new LinkedHashMap<>(16, 0.75f, true); // accessOrder = true

        map.put("apple", 1);
        map.put("banana", 2);
        map.put("cherry", 3);

        // Access some entries
        map.get("banana");
        map.get("apple");

        System.out.println("Iteration in access order:");
        for (Map.Entry<String, Integer> entry : map.entrySet()) {
            System.out.println(entry.getKey() + " => " + entry.getValue());
        }
    }
}

🧾 Output:

Iteration in access order:
cherry => 3
banana => 2
apple => 1

✅ Notice: cherry comes first because it wasn’t accessed.
banana and apple moved to the end after being read.

🧠 TL;DR:

  • LinkedHashMap preserves order:
    • By insertion (default) 🧱
    • Or by access (if configured) 🔄
  • Perfect when you need both speed of HashMap and predictable iteration order.
This entry was posted in Без рубрики. Bookmark the permalink.

Leave a Reply

Your email address will not be published.