0 votes
1 view
in Java by (4.1k points)
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?

1 Answer

0 votes
by (46.1k points)

Use LinkedHashMap.

It is precisely like HashMap, except that if you iterate over it, it presents the items in the injection order.

Example: 

void foo(Map m) {

         Map copy = new LinkedHashMap(m);

         ...

     }

Related questions

0 votes
1 answer
0 votes
1 answer
asked Aug 26, 2019 in Java by Krishna (2.6k points)
0 votes
1 answer
0 votes
1 answer
asked Jul 15, 2019 in Java by Nigam (4.1k points)
Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...