Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 05-08-2009, 07:13 PM
xcallmejudasx's Avatar
Senior Member
 
Join Date: Oct 2008
Location: Houston, TX & Flint, MI
Posts: 585
Rep Power: 2
xcallmejudasx is on a distinguished road
Send a message via AIM to xcallmejudasx
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.
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"));	
	}						
}
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
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
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() );
}
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.
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 05-08-2009, 07:38 PM
OrangeDog's Avatar
Senior Member
 
Join Date: Jan 2009
Location: Cambridge, UK
Posts: 838
Rep Power: 2
OrangeDog is on a distinguished road
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.

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
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 05-10-2009, 08:01 PM
xcallmejudasx's Avatar
Senior Member
 
Join Date: Oct 2008
Location: Houston, TX & Flint, MI
Posts: 585
Rep Power: 2
xcallmejudasx is on a distinguished road
Send a message via AIM to xcallmejudasx
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.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 05-11-2009, 12:35 AM
OrangeDog's Avatar
Senior Member
 
Join Date: Jan 2009
Location: Cambridge, UK
Posts: 838
Rep Power: 2
OrangeDog is on a distinguished road
Default
Then you want something like
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
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Retaining DB values as well as Dynamically generated Values.. Help Needed ! rajivjha Advanced Java 0 05-22-2008 11:53 AM
Accessing boolean Values of another values in one class. a_iyer20 Advanced Java 4 04-15-2008 02:04 PM
HashMap: Obtaining all values in a collision? markus-sukram New To Java 2 03-29-2008 11:25 PM
How to make a hashmap to allow duplicate values? Preethi New To Java 0 02-08-2008 01:35 PM
how to return values from hashmap oregon New To Java 2 08-01-2007 05:56 PM


All times are GMT +2. The time now is 10:53 PM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org