Guest User

Untitled

a guest
Jun 24th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.11 KB | None | 0 0
  1. package com.mov;
  2.  
  3. import java.util.*;
  4.  
  5. public class MapMovieYearFinder implements MovieYearFinder {
  6. private final SortedMap<Integer,List<Movie>> movies=new TreeMap<>();
  7.  
  8. public void add(Movie movie){
  9. if (movies.containsKey(movie.getYear())) {
  10. List<Movie> list = movies.get(movie.getYear());
  11. list.add(movie);
  12. movies.put(movie.getYear(), list);
  13.  
  14. } else {
  15. movies.put(movie.getYear(), new ArrayList<>(Arrays.asList(movie)));
  16. }
  17.  
  18. }
  19.  
  20. @Override
  21. public List<Movie> searchByYear(int year) {
  22. if(movies.containsKey(year)) {
  23. List<Movie> found = new ArrayList<>();
  24. found.addAll(movies.get(year));
  25.  
  26. return found;
  27. }
  28. else throw new IllegalArgumentException("No movies this year");
  29. }
  30.  
  31. @Override
  32. public List<Movie> searchByYears(int year1, int year2) {
  33. List<List<Movie>>list = new ArrayList<>(movies.subMap(year1,year2+1).values());
  34. List<Movie> found = new ArrayList<>();
  35. for (List<Movie> f : list) {
  36. found.addAll(f);
  37. }
  38. return found;
  39. }
  40. }
Add Comment
Please, Sign In to add comment