www.webdeveloper.com
Results 1 to 2 of 2

Thread: Simple HashMap question

  1. #1
    Join Date
    Oct 2005
    Posts
    19

    Simple HashMap question

    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!

  2. #2
    Join Date
    May 2006
    Posts
    1
    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());

    }

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles