Results 1 to 4 of 4
  1. #1
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    7

    Default HashMap contains all values but doesn't show all values

    I'm a little confused as to why my map.getValue() only returns [Drafter] even though every test I've done using loops and list.get() return [Developer,Drafter]?

    Here is my code with a better explanation of what is happening.
    Java Code:
    //resultSet contains 2 group names and both are sucessfully added to the groups list and displayed using MapEntry.getKey()
    resultSet.beforeFirst();
    while(resultSet.next()){
    	if(!groups.contains(rs.getString("GROUP_FULLNAME"))){
    //	System.out.println("Adding to groups: "+rs.getString("GROUP_FULLNAME"));
            groups.add(resultSet.getString("GROUP_FULLNAME"));	
    	}						
    }
    Java Code:
    //roleNames is a result that contains all the roles when groupName = groups.get(xx)
    for(int xx = 0; xx < groups.size(); xx++){
    	if(!roleNames.first()){
    			throw new Exception("role_name query failed!");
    	}
    	roleNames.beforeFirst();
    	rolesList.clear();
    	while(roleNames.next()){
    	System.out.println("Adding role: "+roleNames.getString("ROLE_NAME")+" to group: "+groups.get(xx));
    	rolesList.add(roleNames.getString("ROLE_NAME"));
    	}
    	//puts the key and value together in the map
    	System.out.println(groups.get(xx)+" : "+rolesList.size());
    	for(int i = 0; i<rolesList.size(); i++){
    		System.out.println(groups.get(xx)+" ...:..."+rolesList.get(i));
    	}
    	groupRoleInfoMap.put(groups.get(xx), rolesList);
    }
    now the output for all that seems fine
    Java Code:
    Adding role: Developer to group: Corporate Engineering Technology
    Adding role: Drafter to group: Corporate Engineering Technology
    Corporate Engineering Technology : 2
    Corporate Engineering Technology ...:...Developer
    Corporate Engineering Technology ...:...Drafter
    Adding role: Drafter to group: Rig Solutions.RS Mechanical.Mud Pumps
    Rig Solutions.RS Mechanical.Mud Pumps : 1
    Rig Solutions.RS Mechanical.Mud Pumps ...:...Drafter
    however when I use this code the output is different and doesn't show all roles
    Java Code:
    Set set = groupRoleInfoMap.entrySet();
    Iterator i = set.iterator();
    
    while(i.hasNext()){
          Map.Entry me = (Map.Entry)i.next();
          System.out.println(me.getKey() + " : " + me.getValue() );
    }
    Java Code:
    Corporate Engineering Technology : [Drafter]
    Rig Solutions.RS Mechanical.Mud Pumps : [Drafter]
    I'm assuming all the data is there but some reassurance would be nice. Have I done something wrong within the iterator loop or what?
    Liberty has never come from the government.
    Liberty has always come from the subjects of government.
    The history of liberty is the history of resistance.
    The history of liberty is a history of the limitation of governmental power, not the increase of it.

  2. #2
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    A map cannot have repeated keys. When you add a CET role the second time, it overwrites the first one. You probably want a map containing lists of values. e.g. Map<String,ArrayList<String>>

    Using for-each loops and genericised collections will help with programming, readability and reduce errors.

    Java Code:
    for (Map.Entry<String,String> me : groupRoleInfoMap.getEntrySet() {
        System.out.println(me.getKey() + " : " + me.getValue() );
    }
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

  3. #3
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    7

    Default

    I wasn't aware I was repeating the keys. My map is already a <String, List<String>>. Logically everything seems to follow and work correctly but if you could point out any flaws I'd appreciate it.
    groupRoleInfoMap.put only gets called once per group(the outer loop) and adds the groupName and the list of roles within that group(determined by inner loop). It seems so simple and I can't understand where I'm going wrong.
    Liberty has never come from the government.
    Liberty has always come from the subjects of government.
    The history of liberty is the history of resistance.
    The history of liberty is a history of the limitation of governmental power, not the increase of it.

  4. #4
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    Then you want something like
    Java Code:
    groupRoleInfoMap.get(xx).add(rolesList);
    every time after the first occurrence of each key
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

Similar Threads

  1. Replies: 0
    Last Post: 05-22-2008, 11:53 AM
  2. Replies: 4
    Last Post: 04-15-2008, 02:04 PM
  3. HashMap: Obtaining all values in a collision?
    By markus-sukram in forum New To Java
    Replies: 2
    Last Post: 03-29-2008, 11:25 PM
  4. How to make a hashmap to allow duplicate values?
    By Preethi in forum New To Java
    Replies: 0
    Last Post: 02-08-2008, 01:35 PM
  5. how to return values from hashmap
    By oregon in forum New To Java
    Replies: 2
    Last Post: 08-01-2007, 05:56 PM

Posting Permissions

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