Results 1 to 4 of 4
- 03-15-2011, 02:12 PM #1
Member
- Join Date
- Jan 2011
- Posts
- 71
- Rep Power
- 0
Maps and Sets, why wont this work?
Hello again folks
Ok, here is my problem:
I have a Map called results. This map holds a Character as it's key and a Set of type String as it's value. When I find the character (from another map, based on imaginery student grades) I want to put the name of that student into the corresponding set with the key matching the Character.
Here's my method
I've tested the if statement, and it is comparing the Character to the key correctly, however, when I then want to add the name (aName) to the corresponding set the method instead adds the name to ALL sets.Java Code:public void collateResults() { char grades[] = {'A', 'B', 'C', 'D', 'F', 'X'}; Set<String> resultSet = new HashSet <String>(); for(int i = 0; i < grades.length; i++) { this.results.put(grades[i], resultSet); } Set<String> key = students.keySet(); for (String theStudents : key) { Character aChar = this.students.get(theStudents).getGrade(); String aName = this.students.get(theStudents).getName(); Set<Character> keys = results.keySet(); for(Character allGrades : keys) { if(allGrades.equals(aChar)) { resultSet = this.results.get(aChar).add(aName); } } } }
What's wrong?
Thanks
- 03-15-2011, 02:16 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,413
- Blog Entries
- 7
- Rep Power
- 17
This is where you go wrong, all the keys are associated with the same HashSet. Better make that:
kind regards,Java Code:char grades[] = {'A', 'B', 'C', 'D', 'F', 'X'}; for(int i = 0; i < grades.length; i++) { this.results.put(grades[i], new HashSet<String>()); }
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 03-15-2011, 02:21 PM #3
Member
- Join Date
- Jan 2011
- Posts
- 71
- Rep Power
- 0
- 03-15-2011, 03:01 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,413
- Blog Entries
- 7
- Rep Power
- 17
When people rob a bank they get a penalty; when banks rob people they get a bonus.
Similar Threads
-
Why Wont This Work???
By Billywizz in forum New To JavaReplies: 11Last Post: 03-09-2011, 02:33 AM -
why wont the %.2f work here?
By jjth39347 in forum New To JavaReplies: 2Last Post: 03-06-2011, 05:55 AM -
Maps and Sets
By darkblue24 in forum New To JavaReplies: 19Last Post: 03-25-2010, 06:13 PM -
Maps and Sets
By RedKMan in forum New To JavaReplies: 3Last Post: 02-16-2010, 09:36 AM -
how does the remove method work for sets and hashsets
By haridharna in forum Advanced JavaReplies: 4Last Post: 08-06-2007, 12:48 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks