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.

NewBee: HashMaps and an Array

Thread view: 
psmith@mcwy.com - 11 Mar 2007 21:42 GMT
This is driving me mad.  Can somebody please show me where I am going
wrong here.

I have two hashmaps, one called Students, which contains some results
The one I am having problems with is called results.  I need to know
how to iterate through an array to set the grade up as a key in the
hashmap.  This is the element of code I am struggling with:

public class TutorGroup
{
  /* instance variables */
  Map<String, Student> students;
  Map<Character, Set <String>> results;

  /**
   * Constructor for objects of class TutorGroup
   */
  public TutorGroup()
  {
    this.students = new HashMap<String, Student>();
    this.results = new HashMap<Character, Set<String>>();
  }

public void collateResults()
  {
     char grades[] = {'A', 'B', 'C', 'D', 'F', 'X'};

       for (int i = 0; i < grades.length; i++)
    }
       results.add(grades[i], " ");
    }

The value element of the results map will eventually hold the student
who has achieved the respective grade.

Any help received greatly appreciated.

Paul
Tom Hawtin - 11 Mar 2007 22:39 GMT
> I have two hashmaps, one called Students, which contains some results
> The one I am having problems with is called results.  I need to know

It would make it simpler, if you reduced your problem to just the bit
you are having the problem with. If you deleted students from your code,
 you wouldn't even need to explain that you don't have a problem with it.

> how to iterate through an array to set the grade up as a key in the
> hashmap.  This is the element of code I am struggling with:
>
> public class TutorGroup

>    Map<Character, Set <String>> results;
    ^private final

>    public TutorGroup()
>    {
[quoted text clipped - 12 lines]
> The value element of the results map will eventually hold the student
> who has achieved the respective grade.

So you are looking to add mappings to (initially) empty sets here?

Rather than making "init" methods public, it's better to (effectively)
add them to the constructor. Also an enum would be a better fit than
char literals. So:

enum Grade {
    A, B, C, D, F, X
}

    public TutorGroup() {
        this.results =
            new java.util.EnumMap<Character, Set<String>>(Grade.class);
        for (Grade grade : Grade.values()) {
            this.results.put(grade, new java.util.HashSet<String>());
        }
    }

Unfortunately the Java library does not come with support for
multimaps[1]. Writing a multimap yourself might be overkill, but you can
factor out the initialisation code for arbitrary enums into a testable
method:

        this.results = SomeOtherClas.createEnumMultimap(Grade.class);
...
    static <K extends Enum<K>,V> Map<T,Set<V>> createEnumMultimap(
        Class<K> keyType
    ) {
         Map<K,V> map = new java.util.EnumMap<K,V>(keyType);
         for (K key : keyType.getEnumConstants()) {
             map.put(key, new java.util.HashSet<V>());
         }
         return map;
    }

EnumMap is a very fast compact Map implementation.

(Disclaimer: I haven't so much as attempted to compile any of this.)

Tom Hawtin

[1] http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4155149 (5 votes)


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.