Link to home
Start Free TrialLog in
Avatar of static86
static86

asked on

hashmap getting key+value

Hi,
Is it possible to iterate through hashmap and get key and value?

Avatar of for_yan
for_yan
Flag of United States of America image



What do you mean by this quuestion?
I guess hashmap is designed for that - you iterate through the keys
and then retrive the value corresoponding to the key
and if you wich you can concatenate them, for example.

Is this somethin that you meant:

HashMap m;

Enumeration keys = m.keySet();
Iterator it = m.iterator();
while(it.hasNext()){
Object ob = it.next();
Object value = m.get(ob);
System.out.println(ob.toString() + "  "  + value.toString());
}



This is with corrections:

       
HashMap m = new HashMap();

Set keys = m.keySet();
Iterator it = keys.iterator();
while(it.hasNext()){
Object ob = it.next();
Object value = m.get(ob);
System.out.println(ob.toString() + "  "  + value.toString());
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of for_yan
for_yan
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of static86
static86

ASKER

Thanks a lot!
If you know you're going to be getting both the key and the value anyway, it's usually better to iterate over HashMap.entrySet() instead.