Using a list inside a map (Java)
dictionary, java, list
Solution
I think you're on the right path, just make sure your movie object implements `equals` and `hashCode` so it can work as a true key for the hash map.
If you want pretty printing just implement the toString method.
public void addGrade(Movie movie, Grade grade) {
if (!gradedMovies.containsKey(movie)) {
gradedMovies.put(movie, new ArrayList());
}
gradedMovies.get(movie).add(grade);
}
hope this helps, Cheers!
Problem
I'm using a HashMap in which I use an ArrayList as a value. Like this: ``` Map<Movie, List<Grades>> gradedMovies = new HashMap(); ``` I'm trying to create a method with which I could iterate through the values to see if a key(movie) already exists. If it does, I would like to add a new value(grade) into the list that is assigned to the particular key(movie). Something like this: ``` public void addGrade(Movie movie, Grade grade) { // stuff here } ``` Ultimately I wan't to be able to print a Map which would display the Movies and its' grades after they've been added to the map. How is this accomplished? Or is my whole approach (using a Map) totally wrong? Thanks for any assistance. (This is homework)