static86
asked on
hashmap getting key+value
Hi,
Is it possible to iterate through hashmap and get key and value?
Is it possible to iterate through hashmap and get key and value?
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.toSt ring() + " " + value.toString());
}
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.toSt
}
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());
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
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.