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
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);
}
}
}
}
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.
What's wrong?
Thanks