• Post Reply Bookmark Topic Watch Topic
  • New Topic

Ordering of HashMap entries ...  RSS feed

 
Ranch Hand
Posts: 1585
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am inserting entries into a HashMap, but, after iterating through the map, the ordering of the elements inserted into the map are not as were entered.

What do I need to do in order to preserve the order ? Do I need to implement the hashCode and equals methods ?

Please note that my map's key type is String and value type is Object.

Thanks in advance ...
 
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use LinkedHashMap - it keeps the elements in the order you inserted them.
 
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HashMap doesn't support ordering of the elements.
Try using LinkedHashMap
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!