Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
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

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...