jaipur07
asked on
procesing HashMap
I have stonred some values in HashMap.
I am having trouble getting values back.
Please shere some code to process HashMap objects...
I am having trouble getting values back.
Please shere some code to process HashMap objects...
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
you simply do Object o = hashmap.get("key"); and this should give you the object that is identified by the key "key"
Cheers
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
Map map = new HashMap();
map.put("Name", "ABC"); // Puts Name to hashmap
String value = (String) map.get("Name"); //Returns the value of Name
ASKER
no no i don't want value based on a key...i want to get all key value pairs from hashmap
Iterator iter = HashMap.keySet().iterator( );
while(iter.hasNext()) {
System.out.println(iter.ne xt());
}
while(iter.hasNext()) {
System.out.println(iter.ne
}
Iterator iter = HashMap.keySet().iterator( );
while(iter.hasNext()) {
String s = iter.next();
System.out.println(s);
System.out.println(HashMap .get(s));
}
while(iter.hasNext()) {
String s = iter.next();
System.out.println(s);
System.out.println(HashMap
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
:)
Hi jaipur07,
You can get keys and values using entrySet() :
Iterator it=hashmap.entrySet().iter ator();
while (it.hasNext())
{
Map.Entry entry=(Map.Entry)it.next() ;
... entry.getKey() // is the key
... entry.getValue() // is the value associated to the key
}
You can get keys and values using entrySet() :
Iterator it=hashmap.entrySet().iter
while (it.hasNext())
{
Map.Entry entry=(Map.Entry)it.next()
... entry.getKey() // is the key
... entry.getValue() // is the value associated to the key
}
ASKER
thanks!!
you guys are awsome...i wish i could award 10000000 points
you guys are awsome...i wish i could award 10000000 points
jaipur07, please try to be more precise in your questions - it will save time.
For getting the pairs, Webstorm's approach is preferable
For getting the pairs, Webstorm's approach is preferable
String X = (String)map.get("X");