Java
Programming
Data Structures
Map Interface
Ordered Map

Java Ordered Map

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

In Java, “ordered map” does not refer to one single class. Usually it means a Map implementation whose iteration order is predictable, and the correct choice depends on what “ordered” means for your use case: insertion order, access order, key-sorted order, or concurrent sorted order.

Why the Term Is Ambiguous

A plain HashMap does not guarantee iteration order. If the code depends on any stable order, you need a map implementation that explicitly defines one.

In practice, Java developers usually mean one of these:

  • 'LinkedHashMap for insertion order'
  • access-ordered LinkedHashMap for cache-like behavior
  • 'TreeMap for key-sorted order'
  • 'ConcurrentSkipListMap when you need sorted keys and concurrent access'

Those are different guarantees. Using the wrong one often creates subtle bugs or unnecessary overhead.

LinkedHashMap for Insertion Order

If you want entries to come back in the same order they were inserted, use LinkedHashMap.

java
1import java.util.LinkedHashMap;
2import java.util.Map;
3
4public class Main {
5    public static void main(String[] args) {
6        Map<Integer, String> map = new LinkedHashMap<>();
7        map.put(3, "third");
8        map.put(1, "first");
9        map.put(2, "second");
10
11        map.forEach((k, v) -> System.out.println(k + " -> " + v));
12    }
13}

The iteration order is 3, 1, 2 because that is the insertion order.

This is useful when you need deterministic output but do not need sorted keys.

Access-Ordered LinkedHashMap

LinkedHashMap can also be configured to reorder entries when they are accessed. That is useful for simple least-recently-used cache patterns.

java
1import java.util.LinkedHashMap;
2
3public class Main {
4    public static void main(String[] args) {
5        LinkedHashMap<Integer, String> map =
6            new LinkedHashMap<>(16, 0.75f, true);
7
8        map.put(1, "A");
9        map.put(2, "B");
10        map.put(3, "C");
11
12        map.get(1);
13        map.get(2);
14
15        map.forEach((k, v) -> System.out.println(k + " -> " + v));
16    }
17}

Now iteration reflects access history rather than original insertion order.

You can build a small LRU cache by overriding removeEldestEntry:

java
1import java.util.LinkedHashMap;
2import java.util.Map;
3
4class LruCache<K, V> extends LinkedHashMap<K, V> {
5    private final int capacity;
6
7    LruCache(int capacity) {
8        super(16, 0.75f, true);
9        this.capacity = capacity;
10    }
11
12    @Override
13    protected boolean removeEldestEntry(Map.Entry<K, V> eldest) {
14        return size() > capacity;
15    }
16}

This is one of the most common reasons Java developers care about ordered maps.

TreeMap for Sorted Keys

If the keys themselves must stay sorted, use TreeMap.

java
1import java.util.Map;
2import java.util.TreeMap;
3
4public class Main {
5    public static void main(String[] args) {
6        Map<String, Integer> scores = new TreeMap<>();
7        scores.put("charlie", 78);
8        scores.put("alice", 91);
9        scores.put("bob", 85);
10
11        scores.forEach((k, v) -> System.out.println(k + " -> " + v));
12    }
13}

This prints keys in sorted order: alice, bob, charlie.

TreeMap is also useful because it supports navigational operations such as:

  • 'firstKey()'
  • 'lastKey()'
  • 'headMap()'
  • 'tailMap()'
  • 'subMap()'

If range queries matter, those methods are often more important than ordering alone.

Concurrent Sorted Order

If you need both sorted-key order and safe concurrent access, ConcurrentSkipListMap is worth knowing.

java
1import java.util.concurrent.ConcurrentSkipListMap;
2
3public class Main {
4    public static void main(String[] args) {
5        ConcurrentSkipListMap<Integer, String> map =
6            new ConcurrentSkipListMap<>();
7
8        map.put(5, "five");
9        map.put(1, "one");
10        map.put(3, "three");
11
12        map.forEach((k, v) -> System.out.println(k + " -> " + v));
13    }
14}

It is not a drop-in replacement for every map, but it is useful when ordering and concurrency both matter.

How to Choose

A practical rule is:

  • use LinkedHashMap if insertion order matters
  • use access-ordered LinkedHashMap if recent access should influence order
  • use TreeMap if keys must stay sorted
  • use ConcurrentSkipListMap when you need sorted keys under concurrent access

That is the real question behind “ordered map”: what ordering rule do you actually need?

Common Pitfalls

The most common mistake is saying “ordered map” without specifying what order is required.

Another issue is relying on HashMap iteration order because it happened to look stable in tests. That is not a supported contract.

People also choose TreeMap when they really only needed insertion order, which adds logarithmic overhead for no benefit.

Finally, remember that TreeMap and ConcurrentSkipListMap need comparable keys or an explicit comparator. Without that, ordering cannot be defined correctly.

Summary

  • In Java, “ordered map” can mean several different ordering guarantees.
  • 'LinkedHashMap preserves insertion order by default.'
  • Access-ordered LinkedHashMap is useful for cache-style behavior.
  • 'TreeMap keeps keys sorted and supports range-style navigation.'
  • Choose the map based on the required ordering rule, not just the word “ordered.”

Course illustration
Course illustration

All Rights Reserved.