This repository acts as a personal archive for my solutions to EdX course *Data Structures and Software Design* from PennX.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

35 lines
1.3 KiB

  1. /*
  2. * SD2x Homework #5
  3. * Implement the method below according to the specification in the assignment description.
  4. * Please be sure not to change the method signature!
  5. */
  6. import java.util.List;
  7. import java.util.PriorityQueue;
  8. import java.util.TreeMap;
  9. public class MovieRatingsParser {
  10. public static TreeMap<String, PriorityQueue<Integer>> parseMovieRatings(List<UserMovieRating> allUsersRatings) {
  11. TreeMap<String, PriorityQueue<Integer>> movieRatings = new TreeMap<String, PriorityQueue<Integer>>();
  12. if(allUsersRatings == null) return movieRatings;
  13. for(UserMovieRating m : allUsersRatings){
  14. if(m == null) continue;
  15. if(m.getMovie() == null) continue;
  16. if(m.getMovie().isEmpty()) continue;
  17. if(m.getUserRating() < 0) continue;
  18. if(!movieRatings.containsKey(m.getMovie().toLowerCase())){
  19. PriorityQueue<Integer> ratingHeap = new PriorityQueue<Integer>();
  20. ratingHeap.add(m.getUserRating());
  21. movieRatings.put(m.getMovie().toLowerCase(), ratingHeap);
  22. continue;
  23. }
  24. movieRatings.get(m.getMovie().toLowerCase()).add(m.getUserRating());
  25. }
  26. return movieRatings;
  27. }
  28. }