• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Retrieve values using hashmap

 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have stored key values in a hash map.

I would now like to retrieve the values back to be printed.
How do I do that?
Please help as I am very new to this field.
Thanks
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look through the Map doc for a method that "returns a set view of the keys contained in this map." Then you can get an iterator on the set and work through the keys one at a time. Does that do what you need?
 
Aparna Ram
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wish I was able to get that onto my head right away.. but I would have preferred some code instead, showing the same.
 
Aparna Ram
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anybody to help please?
 
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stan gave you a very good clue - a method that "Returns a set view of the keys contained in this map."

I'll give you another small hint - the method name is keySet(). Once you have a set of keys from your map, you can iterate over the set of keys and retrieve the values.

Regards,
JD
 
Aparna Ram
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it something like this?

After this, how do I get to display them?
That is where I am stuck!
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or you could just get the list of values directly using the values() method.
[ July 05, 2007: Message edited by: Joanne Neal ]
 
Aparna Ram
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, let me be clearer. This is how it is.

I need to populate a JSF dropdown menu from the database using HashMap.

I get the values from the dB and put them in a hashmap in MemberSearchUtil class, this way:


In the Action class, how do I get the state_list values back is my problem.


Kindly help.
 
John Dell'Oso
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's some code that may help you. It's not a very good example (it's late here Down Under) but I think it sort of demonstrates what you're looking for.



I'm not sure what version of the JDK you're using (or if you use generics), so here's a version without the generics:



Hope this helps.

Regards,
JD
[ July 05, 2007: Message edited by: John Dell'Oso ]
 
Aparna Ram
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I tried what you suggested. In a list of 50, it goes to the 15th key and prints only the 15th value.!! Where am I going wrong??
 
Aparna Ram
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I resolved it. Thanks for all the help.
 
    Bookmark Topic Watch Topic
  • New Topic