Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / March 2007

Tip: Looking for answers? Try searching our database.

Merging HashMaps

Thread view: 
psmith@mcwy.com - 12 Mar 2007 18:11 GMT
I have to merge two hashmaps, one results (contains grades), the other
students(contains students, with exam results.  Their exam marks are
averaged out and graded.  I need the students to appear in the results
hashmap against their grade, can somebody please point out what is
wrong with the following:

public void collateResults()
  {
     char grades[] = {'A', 'B', 'C', 'D', 'F', 'X'};
     {
        for (Character grade : grades)
        {
           this.results.put(grade, new HashSet<String>());
        }
        for (String eachStudent : students.keySet())
        {
           for(String graded : this.results.get(eachStudent))
           {
              this.results.get(graded).add(eachStudent);
           }
        }
     }
}

Any help greatly appreciated.

Paul
Tom Hawtin - 12 Mar 2007 20:37 GMT
>          for (Character grade : grades)
>          {
>             this.results.put(grade, new HashSet<String>());

So the key to results is a Character.

>          }
>          for (String eachStudent : students.keySet())
>          {
>             for(String graded : this.results.get(eachStudent))
>             {
>                this.results.get(graded).add(eachStudent);

And here you are using Strings as the key.

An object cannot be simultaneously a Character and a String, so they
will never match.

(As I said earlier, the code would be much clearer if 'grade' was an
abstract type.)

Tom Hawtin
Mark Rafn - 12 Mar 2007 20:56 GMT
>I have to merge two hashmaps, one results (contains grades), the other
>students(contains students, with exam results.

Smells like homework.  You may get some hints, but you should ask more
specific questions and describe what you've tried and what specific part you
don't understand.

Maps contain keys and values.  You haven't described which is which for these
maps.

>Their exam marks are averaged out and graded.  I need the students to appear
>in the results hashmap against their grade

I'd call this a join (using data in one map to reference another) rather than
a merge (take two similar hashmaps and combine them).  

>public void collateResults()
>   {
[quoted text clipped - 13 lines]
>      }
>}

1) a "results" member variable is usually a sign of broken class design.  If
the class represents something, name the variable better.  If this is a method
that should calculate and return, but not keep the results, then maybe
 public Map<Char, Set<String>> getStudentsByGrade(Map<String, Char> students)
is a better signature.

2) you're trying to access the "results" map in two different ways.  In one
spot it looks like it's keyed by grade, in another by student.
--
Mark Rafn    dagon@dagon.net    <http://www.dagon.net/>
Gagan - 13 Mar 2007 17:27 GMT
Here is what you could do:

1. Preparae various lists of like graded students (you may itrerate
the existing students set and group like graded students in various
lists). Am assuming student does contain grade variable inside it.
2. Have a hashmap (grade as key and list as value), the list is
reference to the list of correponding like graded students list.

Gagan


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.