wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes [Solved] HashMap, Which thing defines the order of elements Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "[Solved] HashMap, Which thing defines the order of elements" Watch "[Solved] HashMap, Which thing defines the order of elements" New topic
Author

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

Vikas Kapoor
Ranch Hand

Joined: Aug 16, 2007
Posts: 1374
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

Joined: Aug 17, 2009
Posts: 75
HashMap sorts by the hash of the keys. Use TreeMap to sort by key values.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61218
    
  66

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,


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Vikas Kapoor
Ranch Hand

Joined: Aug 16, 2007
Posts: 1374
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: [Solved] HashMap, Which thing defines the order of elements