aspose file tools*
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


Win a copy of Java 8 in Action this week in the Java 8 forum!
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: 60071
    
  65

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.
 
Consider Paul's rocket mass heater.
 
subject: [Solved] HashMap, Which thing defines the order of elements
 
Similar Threads
populating drop down list from database table
populating drop down list from database table
tranfer map values from one to another
populating drop down list from database table
What is a data structure for Map that preserves insert order?