This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

HashMap, DropDown List, order of values

 
A Patel
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a HashMap in my action class that looks somewhat like this:

[key1=value1, key2=value2, keyn=valuen, ""=null]

I make this HashMap available in the session and use it to populate my drop down combo box in my JSP.
So the drop-down list looks like so:

key1
key2
keyn
<blank string>

I want to push the <blank string> to the top as the default value for my drop-down list so that my list looks like so;

<blank string>
key1
key2
keyn

How can I achieve this? I'd appreciate any assistance in getting me started to do this.

Thanks
 
Ergin Er
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by A Patel:
I have a HashMap in my action class that looks somewhat like this:

[key1=value1, key2=value2, keyn=valuen, ""=null]

I make this HashMap available in the session and use it to populate my drop down combo box in my JSP.
So the drop-down list looks like so:

key1
key2
keyn
<blank string>

I want to push the <blank string> to the top as the default value for my drop-down list so that my list looks like so;

<blank string>
key1
key2
keyn

How can I achieve this? I'd appreciate any assistance in getting me started to do this.

Thanks


Isn't that the risk you take when using HashMap? if I remember correctly with hashmap you can forget sorting (or first go in, first come out). Maybe an idea to put the ""=null option manually in the dropdown and populate the rest with the hashmap?
Or use a List populated with object (TO or pojo) that has to attributes: the key and the value.
 
David Ulicny
Ranch Hand
Posts: 724
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with Ergin Er. You can sort the HashMap before showing.
There is method keySet, which return Set of keys, you can sort them and then use them to get the values in sorted order by key.
 
A Patel
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let's say I don't need a sorted list. I just need to push the <blank string> to the top. Is there a way to put a key=value pair at the start of the map?
 
Marc Peabody
pie sneak
Sheriff
Posts: 4727
Mac Ruby VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
TreeMap can help with sorting, and most of the time it's a good idea to put these dropdown collections in application scope and reference them from the jsp that way.

As far as blank values, I never add them to my collection. I use my properties file:
dropdown.blank.short = <option value="">--</option>
dropdown.blank.long = <option value="">- Select -</option>

then I can place my bean:message tag above my html ptionsCollection or whatever I use.
 
Aash Patel
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Marc - I ended up going with your suggestion.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic