wood burning stoves 2.0*
The moose likes Struts and the fly likes HashMap, DropDown List, order of values Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "HashMap, DropDown List, order of values" Watch "HashMap, DropDown List, order of values" New topic
Author

HashMap, DropDown List, order of values

A Patel
Greenhorn

Joined: Aug 19, 2005
Posts: 14
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

Joined: Sep 06, 2005
Posts: 60
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

Joined: Aug 04, 2004
Posts: 724
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.


SCJP<br />SCWCD <br />ICSD(286)<br />MCP 70-216
A Patel
Greenhorn

Joined: Aug 19, 2005
Posts: 14
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

Joined: Feb 05, 2003
Posts: 4727

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.


A good workman is known by his tools.
Aash Patel
Greenhorn

Joined: Sep 14, 2005
Posts: 24
Thanks Marc - I ended up going with your suggestion.
 
 
subject: HashMap, DropDown List, order of values