Reputation: 1
I have a database that maps course names to student id numbers. I need to iterate through the map to create another set that contains all students in the database. This is the code I have so far. Any help would be greatly appreciated!!
//return a set of all students in the school
public Set<Integer> allStudents() {
Set<Map.Entry<String,Set<Integer>>> entries = database.entrySet();
Set<Integer> students = new TreeSet<Integer>();
for (Map.Entry<String,Set<Integer>> pair: entries){
students.add();
}
return students;
} // end allStudents
Upvotes: 0
Views: 419
Reputation: 136162
Your code is correct. Here is a version without iteration
public Set<Integer> allStudents() {
return new HashSet<Integer>(database.values());
}
Upvotes: 0
Reputation: 159874
You could do:
for (Map.Entry<String, Set<Integer>> pair : entries) {
students.addAll(pair.getValue());
}
Upvotes: 1