NewBee: HashMaps and an Array

Discussion in 'Java' started by psmith@mcwy.com, Mar 11, 2007.

  1. Guest

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

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

    Any help received greatly appreciated.

    Paul
     
    , Mar 11, 2007
    #1
    1. Advertising

  2. Tom Hawtin Guest

    wrote:
    >
    > 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()
    > {
    > 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, " ");
    > }
    >
    > 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)
     
    Tom Hawtin, Mar 11, 2007
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Maps and Hashmaps

    , May 25, 2004, in forum: Java
    Replies:
    2
    Views:
    2,447
    jAnO!
    May 25, 2004
  2. Roedy Green

    HashCodes and HashMaps

    Roedy Green, Sep 20, 2005, in forum: Java
    Replies:
    2
    Views:
    481
    Roedy Green
    Sep 20, 2005
  3. Replies:
    5
    Views:
    430
  4. Replies:
    1
    Views:
    312
    Oliver Wong
    Feb 27, 2007
  5. Replies:
    10
    Views:
    517
    AmiDaniel
    Feb 28, 2007
Loading...

Share This Page