• 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

Collections HashMap ResultSet HELP!

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
Thanks!!!
Chris - learning Struts through mistakes...
 
Ranch Hand
Posts: 228
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dude,
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.
HTH
Sahil
 
Chris Perth
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Chris
[ October 31, 2003: Message edited by: Chris Perth ]
 
Chris Perth
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
Chris
 
sandy gupta
Ranch Hand
Posts: 228
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?

Sahil
 
Chris Perth
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!!!
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!
Chris
 
reply
    Bookmark Topic Watch Topic
  • New Topic