• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Map Family

 
ajaysharma fbd
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why we don't get order in HashMap.
I know it uses the hashcode but what differnce did it make to the order.
Here By order I mean elements that are added first to the HashMap should be added first I am not talking about sorting.
 
Junilu Lacar
Bartender
Posts: 7602
54
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The implementation is based on a hash function, which is favorable for speed. Sorting/ordering is a different concern that limits the ability to make things work fast. HashMap favors speed. If you need to preserve order, use a SortedMap implementation.
 
Stephan van Hulst
Bartender
Pie
Posts: 6127
74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
EnumMap and LinkedHashMap are also ordered. SortedMap also keeps elements sorted though.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic