Back

Explore Courses Blog Tutorials Interview Questions
0 votes
1 view
in Java by (4k 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 (46k 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
0 votes
1 answer
asked Aug 26, 2019 in Java by Krishna (2.6k points)
0 votes
1 answer
Welcome to Intellipaat Community. Get your technical queries answered by top developers!

28.4k questions

29.7k answers

500 comments

94k users

Browse Categories

...