Results 1 to 4 of 4
- 05-08-2009, 06:13 PM #1
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")); } }now the output for all that seems fineJava 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); }
however when I use this code the output is different and doesn't show all rolesJava 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
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() ); }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?Java Code:Corporate Engineering Technology : [Drafter] Rig Solutions.RS Mechanical.Mud Pumps : [Drafter]
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.
- 05-08-2009, 06:38 PM #2
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
- 05-10-2009, 07:01 PM #3
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.
- 05-10-2009, 11:35 PM #4
Then you want something like
every time after the first occurrence of each keyJava Code:groupRoleInfoMap.get(xx).add(rolesList);
Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
How To Ask Questions The Smart Way
Similar Threads
-
Retaining DB values as well as Dynamically generated Values.. Help Needed !
By rajivjha in forum Advanced JavaReplies: 0Last Post: 05-22-2008, 10:53 AM -
Accessing boolean Values of another values in one class.
By a_iyer20 in forum Advanced JavaReplies: 4Last Post: 04-15-2008, 01:04 PM -
HashMap: Obtaining all values in a collision?
By markus-sukram in forum New To JavaReplies: 2Last Post: 03-29-2008, 10:25 PM -
How to make a hashmap to allow duplicate values?
By Preethi in forum New To JavaReplies: 0Last Post: 02-08-2008, 12:35 PM -
how to return values from hashmap
By oregon in forum New To JavaReplies: 2Last Post: 08-01-2007, 04:56 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks