Two Laptop Bag*
The moose likes Beginning Java and the fly likes Retrieve values using hashmap Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Retrieve values using hashmap" Watch "Retrieve values using hashmap" New topic
Author

Retrieve values using hashmap

Aparna Ram
Ranch Hand

Joined: Jan 27, 2006
Posts: 59
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
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
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?


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Aparna Ram
Ranch Hand

Joined: Jan 27, 2006
Posts: 59
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

Joined: Jan 27, 2006
Posts: 59
Anybody to help please?
John Dell'Oso
Ranch Hand

Joined: Apr 08, 2004
Posts: 130
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

Joined: Jan 27, 2006
Posts: 59
Is it something like this?

After this, how do I get to display them?
That is where I am stuck!
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3602
    
  15
Or you could just get the list of values directly using the values() method.
[ July 05, 2007: Message edited by: Joanne Neal ]

Joanne
Aparna Ram
Ranch Hand

Joined: Jan 27, 2006
Posts: 59
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

Joined: Apr 08, 2004
Posts: 130
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

Joined: Jan 27, 2006
Posts: 59
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

Joined: Jan 27, 2006
Posts: 59
I resolved it. Thanks for all the help.
 
GeeCON Prague 2014
 
subject: Retrieve values using hashmap