javanewbie
javanewbie

Reputation: 1

Java Collections - Maps and Sets: put cannot be applied

I'm having trouble with some code. I am trying to create a student database. I need to create a set of students that is represented by a course name. The course name is mapped to the set of students. I've tried to write the 'add' method but when I try to .put it into the database I get an error message: put(java.lang.String,java.util.Set) in java.util.Map> cannot be applied to (java.lang.Integer,StudentDatabase.Student). Any help is greatly appreciated!!!!

import java.util.*;

public class StudentDatabase {

private Map<String, Set<Integer>> database = new TreeMap<String, Set<Integer>>();

private static class Student extends TreeSet<Integer> {
    public int id;

    public Student(int id){
        this.id = id;
    }
}

public void add(String courseName, Integer student) {
    /* I've tried to use this way to add to the database and it doesn't work too.
    Set<Integer> studentSet = database.get(courseName);
    if (studentSet == null){
        studentSet = new TreeSet<Integer>();
    }
    studentSet.add(student);
    database.put(courseName, student);
    */

   Integer idInt = new Integer(idInt);
   if (database.containsKey(idInt)){
       //if the student is a duplicate, that is ok
    }
    else{
        Student info = new Student(idInt);
        database.put(new Integer(idInt), info);
    }
} // end add

}

Upvotes: 0

Views: 2890

Answers (2)

Reimeus
Reimeus

Reputation: 159874

You need to match the parameter types of Map#put with your declaration Map<String, Set<Integer>>:

Student info = new Student(idInt);
Set<Integer> searchMap = database.get(courseName);
if (searchMap == null) {
   searchMap = new HashSet<String>();
}

searchMap.add(new Integer(idInt));
database.put(courseName, searchMap);

Upvotes: 0

jtahlborn
jtahlborn

Reputation: 53694

The answer is right in the error message: put(java.lang.String,java.util.Set) in java.util.Map> cannot be applied to (java.lang.Integer,StudentDatabase.Student). which type in the second list doesn't match the type in the first list?

Upvotes: 0

Related Questions