Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 295
  • Last Modified:

procesing HashMap

I have stonred some values in HashMap.
I am having trouble getting values back.

Please shere some code to process HashMap objects...
0
jaipur07
Asked:
jaipur07
  • 5
  • 2
  • 2
  • +2
1 Solution
 
CEHJCommented:
Say you want String 'X':


String X = (String)map.get("X");
0
 
girionisCommented:
Hi jaipur07

you simply do Object o = hashmap.get("key"); and this should give you the object that is identified by the key "key"

Cheers
0
 
Ajay-SinghCommented:
If you have declared HashMap as

Map map = new HashMap();
map.put("Name", "ABC"); // Puts Name to hashmap
String value = (String) map.get("Name"); //Returns the value of Name
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
jaipur07Author Commented:
no no i don't want value based on a key...i want to get all key value pairs from hashmap
0
 
girionisCommented:
Iterator iter = HashMap.keySet().iterator();
while(iter.hasNext()) {
    System.out.println(iter.next());
}
0
 
girionisCommented:
Iterator iter = HashMap.keySet().iterator();
while(iter.hasNext()) {
    String s = iter.next();
    System.out.println(s);
    System.out.println(HashMap.get(s));
}
0
 
girionisCommented:
this

> String s = iter.next();

needs a cast

String s = (String) iter.next();

0
 
girionisCommented:
:)
0
 
WebstormCommented:
Hi jaipur07,

You can get keys and values using entrySet() :

Iterator it=hashmap.entrySet().iterator();
while (it.hasNext())
{
     Map.Entry entry=(Map.Entry)it.next();
      ...  entry.getKey()         // is the key
      ...  entry.getValue()      // is the value associated to the key
}
0
 
jaipur07Author Commented:
thanks!!
you guys are awsome...i wish i could award 10000000 points
0
 
CEHJCommented:
jaipur07, please try to be more precise in your questions - it will save time.

For getting the pairs, Webstorm's approach is preferable
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 5
  • 2
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now