Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.#.movies;
- import java.util.*;
- import java.util.stream.Collectors;
- public class MoviesAnalyzer {
- static class Movie {
- String title;
- String duration;
- List<String> actors;
- List<Integer> watchlist;
- List<Integer> favorites;
- List<Rating> ratings;
- }
- static class Rating {
- int userId;
- int rating;
- }
- static class User {
- int userId;
- String email;
- List<Integer> friends;
- }
- List<Movie> movies;
- List<User> users;
- public MoviesAnalyzer(List<Movie> movies, List<User> users) {
- this.movies = movies;
- this.users = users;
- }
- public List<String> topFavouriteMoviesAmongFriends(int userId) {
- List<String> movies = new ArrayList<>();
- HashMap<String, Long> ranking = new HashMap<>();
- Optional<User> user = findUser(userId);
- user.ifPresent(u -> this.movies.stream().forEach(movie -> {
- Long friendsRating = movie.favorites.stream()
- .filter(favorite -> u.friends.contains(favorite)).count();
- ranking.put(movie.title, friendsRating);
- }));
- ranking.entrySet().stream()
- .sorted(Map.Entry.comparingByKey())
- .sorted(Map.Entry.comparingByValue(Comparator.reverseOrder()))
- .forEach(r -> movies.add(r.getKey()));
- return movies.subList(0, 3);
- }
- private Optional<User> findUser(int userId) {
- for(int i = 0; i < this.users.size(); i++) {
- if (this.users.get(i).userId == userId) {
- return Optional.of(this.users.get(i));
- }
- }
- return Optional.empty();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement