• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 1374
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HashMap sorts by the hash of the keys. Use TreeMap to sort by key values.
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic