HiveBrain v1.2.0
Get Started
← Back to all entries
snippetjavaCritical

How to preserve insertion order in HashMap?

Submitted by: @import:stackoverflow-api··
0
Viewed 0 times
howinsertionpreservehashmaporder

Problem

I'm using a HashMap. When I iterate over the map, the data is returned in (often the same) random order. But the data was inserted in a specific order, and I need to preserve the insertion order. How can I do this?

Solution

LinkedHashMap is precisely what you're looking for.

It is exactly like HashMap, except that when you iterate over it, it presents the items in the insertion order.

Context

Stack Overflow Q#10710193, score: 1267

Revisions (0)

No revisions yet.