How to preserve insertion order in HashMap?
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
In Java, HashMap does not preserve insertion order. If you put keys into a HashMap in one order and iterate later, the iteration order is unspecified and can change as the map grows or is rehashed.
If you need stable insertion order, the usual answer is not to force HashMap into behaving differently. It is to use LinkedHashMap, which is specifically designed to keep entries in insertion order while still offering hash-table lookup performance.
Why HashMap Does Not Preserve Order
HashMap is optimized around hashing and bucket storage, not around predictable iteration order. That means code like this should not be relied on for ordered output:
The code compiles and runs, but the iteration order is not a contract. If your logic depends on insertion order, HashMap is the wrong data structure.
Use LinkedHashMap Instead
LinkedHashMap extends the hash-map idea with a linked structure that records iteration order. The simplest insertion-order version looks like this:
This reliably prints the entries in the order they were inserted:
That is the standard answer whenever people ask how to preserve insertion order in a hash-based map.
Understand Reinsertions and Access Order
There are two useful subtleties to know.
First, reinserting an existing key with put updates the value but does not create a second position in the order. The key keeps its original insertion slot.
Second, LinkedHashMap can also be configured for access order instead of insertion order. That is useful for cache-like behavior:
With accessOrder=true, the order reflects recent access rather than original insertion. That is different from the question here, but it is an important feature of the same class.
Use It for More Than Pretty Iteration
Insertion order matters in several practical situations:
- generating stable JSON or log output
- preserving user-defined option order
- building deterministic tests
- keeping unique items in the order first seen
For example, if you want unique strings while preserving the order they first appeared:
This prints the unique values in first-seen order.
Common Pitfalls
The most common mistake is assuming the current iteration order of a HashMap is stable just because it "looks right" in a test run. That behavior is not guaranteed.
Another common issue is choosing TreeMap when the real requirement is insertion order rather than sorted order. TreeMap sorts by key, which is a completely different behavior.
People also forget about the access-order constructor on LinkedHashMap and accidentally create cache-like iteration when they really wanted insertion order.
Finally, remember that preserving order has a small overhead. In most applications the tradeoff is worth it, but it is still a deliberate data-structure choice.
Summary
- '
HashMapdoes not preserve insertion order.' - Use
LinkedHashMapwhen insertion order matters. - Reinserting an existing key updates the value without creating a new position.
- '
LinkedHashMapcan also be configured for access order, which is different from insertion order.' - Pick the map type based on the order semantics your program actually needs.
Related reading
- How to pretty print nested dictionaries?
- How to prevent going back to the previous activity?
- How to print a dictionary line by line in Python?
- How to print a dictionary's key?
- How to pretty print XML from Java?
- How to prevent embedded netty server from starting with spring-boot-starter-webflux?
- How to print binary tree diagram in Java?
- How to print call stack in Swift?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.