Results 1 to 4 of 4
  1. #1
    jazzermonty is offline Member
    Join Date
    Jan 2011
    Posts
    71
    Rep Power
    0

    Default 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

    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);
                      }                 
                   }
              }                   
       }
    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

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,352
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by jazzermonty View Post
    Java Code:
          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);          
           }
    This is where you go wrong, all the keys are associated with the same HashSet. Better make that:

    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>());          
           }
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    jazzermonty is offline Member
    Join Date
    Jan 2011
    Posts
    71
    Rep Power
    0

    Talking

    Quote Originally Posted by JosAH View Post
    This is where you go wrong, all the keys are associated with the same HashSet. Better make that:

    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>());          
           }
    kind regards,

    Jos


    Jos, you are a star. Thank you sooooo much.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,352
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by jazzermonty View Post
    Jos, you are a star. Thank you sooooo much.
    Java is not like C++; C++ makes a copy of an object when you stick that object in a container; Java doesn't do anything 'behind your back', i.e. the language is way too simple to do such things.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Why Wont This Work???
    By Billywizz in forum New To Java
    Replies: 11
    Last Post: 03-09-2011, 02:33 AM
  2. why wont the %.2f work here?
    By jjth39347 in forum New To Java
    Replies: 2
    Last Post: 03-06-2011, 05:55 AM
  3. Maps and Sets
    By darkblue24 in forum New To Java
    Replies: 19
    Last Post: 03-25-2010, 06:13 PM
  4. Maps and Sets
    By RedKMan in forum New To Java
    Replies: 3
    Last Post: 02-16-2010, 09:36 AM
  5. how does the remove method work for sets and hashsets
    By haridharna in forum Advanced Java
    Replies: 4
    Last Post: 08-06-2007, 12:48 PM

Tags for this Thread

Posting Permissions

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