This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Servlets and the fly likes List boxes 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 » Java » Servlets
Bookmark "List boxes" Watch "List boxes" New topic
Author

List boxes

Brian Nice
Ranch Hand

Joined: Nov 02, 2000
Posts: 195
I would like to be able to create list boxes in a JSP page without having to hit the database to load the list box everytime the user goes to that page. So the list would be generated once via SQL when the user first visits the page, and stored somewhere, probably in a collection item, and then could be used probably via a custom tag anywhere in JSP pages.
The criteria that I would like are:
1. easy to create the drop down list
2. can supply criteria so only certain items get added to the list (i.e. all items for a particular person)
3. can add/remove items from the list box easily
4. ordered by the text that is displayed by the list box
5. be able to supply a value that would represent the item that is initally selected in the list box
I was thinking that I could do one database hit when the user visits the site that would load a HashMap of what I would like to display in the list box. However, most list boxes are ordered (like a drop down list for Countries should be alphabetical) and HashMaps you can order by Key, but not by Value (which in this case would be the country name) You can take a HashMap and get a Set view of it using entrySet(), but as far as I can tell that really doesn't help except in iterating through the Map.
By using a Map, it satisfies all other criteria except for the sorting:
-it's easy to add/remove things
-one could supply a value to match against by iterating through the Map (by using the Set view)
-when creating the Map you could specify the criteria for putting items in the Map (where clause of the SQL)
-easy to create
Is there a way to sort the HashMap by the value and not the key? or is there another way that someone has done something similar that satisfies all of the above criteria?
Open to any and all suggestions!
Thanks for your help!
Brian
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
If I am right in assuming that a dropdown list won't have a very large number of items in it, then it might make sense to sort the elements as they are rendered. I have written and used a simple "SortedEnumeration" class which pops the values into a vector, sorts them and returns an enumeration of the sorted elements, and I'm sure you could do the same. The built in java sorting routines are pretty good for this kind of application.

Read about me at frankcarver.me ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
Brian Nice
Ranch Hand

Joined: Nov 02, 2000
Posts: 195
By sorting the values in the vector, how are you able to also keep the value's key associated with it? That's why I had initally thought of using the map since the HTML <INPUT> tag needs a value too. Am I just missing something?
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
OK. A few other choices:
First, are these value unique, or can the same value appear more than once with different names (seems odd for a dropdown, but possible)?
If they are unique, why not have your map the other way round with the value as the key, and the name as the value. Do you use the map for any other sort of lookup?
If they are not unique, maybe use a Vector (or ArrayList etc.) of name,value pairs rather than a map, and sort that as appropriate.
If you need the lookup capabilities of the map, and the values are unique, how about having two maps, one name->value and one value->name. It shouldn't take up much more space, as all the keys and values will be shared, but would mean that you could access either sorted by key, sorted by value, direct to named key or direct to named value.
is any of this helpful?
Brian Nice
Ranch Hand

Joined: Nov 02, 2000
Posts: 195
Reversing how I store information in the HashMap may work- I hadn't thought of that. Is there any kind of problem with using a long string for the map key (like performance issues)? Most of the values I would be storing are descriptions, so it could happen that I have a string of 50+ characters as the key. I guess if that wouldn't matter to the map, then that could work. However, I cannot guarantee that all descriptions would be unique... :-(
I thought about ArrayList, but wasn't sure how that would work since a List allows you store an object at a specific list index, so there is no real way to keep the key associated with the value if the value were the object. And the key may not always be something that could be converted to an integer if the key were to be used as the array index.
How do you keep the values associated with what is displayed when you create HTML lists with your method?
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
How about something like:
Brian Nice
Ranch Hand

Joined: Nov 02, 2000
Posts: 195
I think that will work! Just one correction I think, the getValue(int i) method I think should return getPair(i).value and not getPair(i).name?
Thanks for your help! I've been working on trying to find something flexible to use for the various projects that I am working on. It should be easy now to create a HashMap containing multiple ObjectLists, and then in a JSP page get out the ObjectList that I need from the HashMap, pass it as a parameter to a JSP Custom tag that then creates the drop down lists, with the list being in the correct order!
Thanks again
Brian
srilatha sarma
Greenhorn

Joined: Nov 07, 2000
Posts: 3
Frank,
How about using a Treeset class for sorting. Please clarify.
Thanks.
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
You could use that. The reason I chose the approach above is that for most uses of an option list, you only need to sort the elements once, after they have all been inserted, but they will be read out of the list lots of times. Therefore it makes sense to limit the "cost" of the sort operation, and use quicker add/get operations while building or reading the list.
TreeSet is very good for collections which change a lot, maybe as often as or more often than they are read, but need to always be sorted. The cost of sorting is spread between both the add and get operations, but there is always an overhead, even if the set has not changed for a long time.
And yes, Brian, you were right about getPair(i).name. That'll teach me to cut-n-paste!
 
Don't get me started about those stupid light bulbs.
 
subject: List boxes