/* * SD2x Homework #5 * Implement the methods below according to the specification in the assignment description. * Please be sure not to change the method signatures! */ import java.util.List; import java.util.PriorityQueue; import java.util.TreeMap; import java.util.Map.Entry; import java.util.Set; import java.util.Stack; import java.util.LinkedList; public class MovieRatingsProcessor { public static List getAlphabeticalMovies(TreeMap> movieRatings) { if(movieRatings == null) return new LinkedList(); return new LinkedList(movieRatings.keySet()); // Normally sets aren't ordered but the docs say the returned set is ordered and it works sooo. \_(._.)_/ } public static List getAlphabeticalMoviesAboveRating(TreeMap> movieRatings, int rating) { List movies = new LinkedList(); if(movieRatings == null) return movies; Set>> entries = movieRatings.entrySet(); for(Entry> e : entries){ if(e.getValue().peek() > rating) movies.add(e.getKey()); } return movies; } public static TreeMap removeAllRatingsBelow(TreeMap> movieRatings, int rating) { TreeMap removed = new TreeMap(); if(movieRatings == null) return removed; int count; PriorityQueue value; LinkedList deleted = new LinkedList(); Integer[] foo = new Integer[1]; for(String n : movieRatings.keySet()){ value = movieRatings.get(n); if(value.peek() >= rating){ continue; } Integer[] ratings = value.toArray(foo); count = 0; for(int i = 0; i < ratings.length; i++){ if(ratings[i] < rating){ value.remove(ratings[i]); count++; } } removed.put(n, count); if(count == ratings.length){ deleted.add(n); } } for(String n : deleted) movieRatings.remove(n); return removed; } }