Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

[Solved] HashMap, Which thing defines the order of elements

 
Vikas Kapoor
Ranch Hand
Posts: 1374
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to populate a drop down box. I fire a query with order by clause. Get list of type List<object[]> in proper order. I prepare Map<String,String> from the list. And populate the drop down from map using ajax(dwr). but drop down has different order, actually unordered. I tried printing Map and found that it didn't preserve the order in which I have inserted the elements. So is there any way through which I can preserve the order?
 
stanislav bashkirtsev
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HashMap sorts by the hash of the keys. Use TreeMap to sort by key values.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64969
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vishal Pandya wrote:So is there any way through which I can preserve the order?


I find java.util.LinkedHashMap useful for holding dropdown values,
 
Vikas Kapoor
Ranch Hand
Posts: 1374
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Spot on.
Thanks Bear.
HashMap sorts by the hash of the keys. Use TreeMap to sort by key values.

Thanks stanislav. I want to sort it by values.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic