Click to See Complete Forum and Search --> : Simple HashMap question


jvanhalderen
05-01-2006, 09:32 AM
Hello, Im using a hashmap to map a counter to certain names. What i want now it print all tag names with assigned values, yet this code print the right value but not the tagname itself but the pointer to the name.

Code (simplified):

HashMap hm = new HashMap();

hm.add ("A", new Integer(1));
hm.add ("B", new Integer(2));
hm.add ("C", new Integer(3));

Set set = hm.keySet();
Iterator iter = set.iterator();
while (iter.hasNext()){
temp1 = hm.get(iter.next()).toString();
temp2 = iter.getValue().toString();
System.out.println(temp1 + " = " + temp2);
}

Wanted output:

A = 1
B = 2
C = 3

Any help would be appreciated!

lithbo
05-01-2006, 09:51 AM
I see what you are trying to do, but you're using methods that don't even exist :)

what you want can be accomplished in many ways, this is the most straight forward...

HashMap hm = new HashMap();

hm.add ("A", new Integer(1));
hm.add ("B", new Integer(2));
hm.add ("C", new Integer(3));

Set set = hm.entrySet();

Iterator iter = set.iterator();

while(iter.hasNext()) {
Map.Entry me = (Map.Entry)iter.next();

System.out.println(me.getKey() + " = " + me.getValue());

}