NewBee: HashMaps and an Array

P

psmith

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
 
T

Tom Hawtin

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)
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,733
Messages
2,569,440
Members
44,830
Latest member
ZADIva7383

Latest Threads

Top