Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.mov;
- import java.util.*;
- public class MapMovieYearFinder implements MovieYearFinder {
- private final SortedMap<Integer,List<Movie>> movies=new TreeMap<>();
- public void add(Movie movie){
- if (movies.containsKey(movie.getYear())) {
- List<Movie> list = movies.get(movie.getYear());
- list.add(movie);
- movies.put(movie.getYear(), list);
- } else {
- movies.put(movie.getYear(), new ArrayList<>(Arrays.asList(movie)));
- }
- }
- @Override
- public List<Movie> searchByYear(int year) {
- if(movies.containsKey(year)) {
- List<Movie> found = new ArrayList<>();
- found.addAll(movies.get(year));
- return found;
- }
- else throw new IllegalArgumentException("No movies this year");
- }
- @Override
- public List<Movie> searchByYears(int year1, int year2) {
- List<List<Movie>>list = new ArrayList<>(movies.subMap(year1,year2+1).values());
- List<Movie> found = new ArrayList<>();
- for (List<Movie> f : list) {
- found.addAll(f);
- }
- return found;
- }
- }
Add Comment
Please, Sign In to add comment