Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
The moose likes Struts and the fly likes Collections HashMap ResultSet HELP! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Collections HashMap ResultSet HELP!" Watch "Collections HashMap ResultSet HELP!" New topic

Collections HashMap ResultSet HELP!

Chris Perth

Joined: Oct 15, 2003
Posts: 21
Hi you guys,
I have this question on my other post from my select options
which has been solved (at least partially)...
Now I'm really struggling with these two issues:
(maybe Yoo-Jin knows this? Hope he's not too busy)
-Is there a direct way to sort the keys/labels alphabetically?!
I'd really love it that the options on my select are organized!
I mean, the select from the database is already distinct and
ordered by that particular row, but still, no matter in what order
I put the stuff into the HashMap, it always lists the content
all scrambled up into my select!!!
(I saw somewhere that I could use a sort method but that it won't
work directly with the Map but rather with the key through an
ArrayList, is that right???!)
-I tried as experiment doing another select (once one worked I
got carried away!) so this time I wanted the colors and the
textures to be taken from the database... but it didn't work!
Maybe it lays on my sql ResultSet (maybe only one can be done
at a time?!), any ideas how I can do this? (I'll need two
HashMaps and two pageContext.setAttributes)
I'd appreciate any help with this! Been two days suffering already!
Chris - learning Struts through mistakes...
sandy gupta
Ranch Hand

Joined: Jan 30, 2001
Posts: 228
HashMap is not an ordered collection and gives no guarantee that the order will be maintained over a period of time. So what you need is some other kind of collection which is inherently ordered in nature. Look at the java.util.List interface and its implementing classes(ArrayList is one of them).
Once you get the sorted list from the database and put it into the Arraylist, you can populate the front end with the arraylist values and order will be retained. On your other post i have code (in jstl, but could be converted to struts html and bean tags or just jsp) where you get the list(ArrayList) out of the scope it is set in(for me its the pageContext which contains the profileCache Object which contains my list) and populate the page with the items from the list.

Chris Perth

Joined: Oct 15, 2003
Posts: 21
Thanks Sahil
I sort of had figures it out already how them maps
don't keep any sorted order... How I wish I'd learned
all this stuff before having to start this project!
Since I just need the labels sorted and not the values,
maybe I'll try out keeping my maps AND an ArrayList to
sort the labels for the options.
I'll certainly check out your code again from my
other post, I'm really thankful for it! (and I'm a
chick nor a dude by the way)
Take care and (if you're into that) happy Halloween
[ October 31, 2003: Message edited by: Chris Perth ]
Chris Perth

Joined: Oct 15, 2003
Posts: 21
I'm still working out with sorting my labels on my
option collections... but the good news is, I got the two
selects I'd been trying with different maps and ResultSets!
It was a stupid sql mistake, typical, me trying things out
again positioning it in the wrong place of the code, but
now it works!
Any ideas or small snippet of code on ArrayList sorting to
be used on my form would still be apreciated.
sandy gupta
Ranch Hand

Joined: Jan 30, 2001
Posts: 228
Hi Chris,
First thing: Really sorry about the dude thing.
Second: You do not have to sort the array list, it will just maintain order. The list that you are getting from the sql, get it in the order that you want and then put it into the arraylist sequentially and that order will be maintained.
Hope that will be the end to all your problems.
And i am into everything so Happy Halloween to you too.
What say to a cpla beers sometime?

Chris Perth

Joined: Oct 15, 2003
Posts: 21
I finally solved my silly problem!
And I didn't need to get rid of my HashMaps either!
It was so easy I'm now feeling really dumb!
With one extra line in my code I used a TreeMap to
sort the HashMap I had and now my select is all
alphabetically organized just like I wanted!
I'll probably post again soon with a new problem,
so stressful this last week... Hope I'll survive...
Take care and thanks for all the help! Gotta run...
Cheers Sahil!
I agree. Here's the link:
subject: Collections HashMap ResultSet HELP!
It's not a secret anymore!