Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.telerikacademy;
- import com.telerikacademy.models.Customer;
- import com.telerikacademy.models.Genre;
- import com.telerikacademy.models.Movie;
- import java.util.Comparator;
- import java.util.List;
- import java.util.stream.Collectors;
- @SuppressWarnings("ForLoopReplaceableByForEach")
- public class DataProcessor {
- public static long countCustomersAboveTargetAge(List<Customer> customers, int targetAge) {
- return customers
- .stream()
- .filter(c -> c.getAge() > targetAge)
- .count();
- }
- public static boolean findIfAllCustomersAreAboveTargetAge(List<Customer> customers, int targetAge) {
- return customers
- .stream()
- .allMatch(c -> c.getAge() > targetAge);
- }
- public static boolean findIfAnyCustomersHasTargetName(List<Customer> customers, String targetName) {
- return customers
- .stream()
- .anyMatch(c -> c.getName().equals(targetName));
- }
- public static boolean findIfAllCustomersDislikeMovie(List<Customer> customers, Movie targetMovie) {
- boolean result = customers
- .stream()
- .allMatch(c -> c.getDislikedMovies().contains(targetMovie));
- return result;
- }
- public static long findHowManyPeopleLikeMove(List<Customer> customers, Movie targetMovie) {
- return customers
- .stream()
- .filter(c -> c.getLikedMovies().contains(targetMovie))
- .count();
- }
- public static double findTheAverageAgeOfPeopleWhoDislikeMovies(List<Customer> customers, Movie targetMovie) {
- List<Customer> customersWhoDislikeMovie = customers
- .stream()
- .filter(c -> c.getDislikedMovies().contains(targetMovie))
- .collect(Collectors.toList());
- return customersWhoDislikeMovie
- .stream()
- .collect(Collectors.averagingDouble(Customer::getAge));
- }
- public static double findAverageAgeOfAllCustomers(List<Customer> customers) {
- return customers
- .stream()
- .collect(Collectors.averagingDouble(Customer::getAge));
- }
- public static List<Customer> findAllCustomersAboveTargetAgeThatLikeGenre(List<Customer> customers, int targetAge, Genre targetGenre) {
- List<Customer> result =
- customers
- .stream()
- .filter(x -> x.getAge() > targetAge)
- .filter(x -> x.getLikedMovies().stream().anyMatch(g -> g.getGenre().equals(targetGenre)))
- .collect(Collectors.toList());
- return result;
- }
- public static List<Customer> findAllCustomersUnderTargetAge(List<Customer> customers, int targetAge) {
- List<Customer> result =
- customers
- .stream()
- .filter(x -> x.getAge() < targetAge)
- .collect(Collectors.toList());
- return result;
- }
- public static Customer findTheCustomerWithTheLongestName(List<Customer> customers) {
- // return customers.stream().max(Comparator.comparing(v -> v.getName().length())).get();
- return customers.stream()
- .reduce(customers.get(0), (s1, s2) -> s1.getName().length() > s2.getName().length() ? s1 : s2);
- }
- public static List<Customer> findAllCustomersWhoLikeOnlyMoviesWithGenre(List<Customer> customers, Genre targetGenre) {
- return customers
- .stream()
- .filter(x -> x.getLikedMovies().stream().allMatch(g -> g.getGenre().equals(targetGenre)))
- .collect(Collectors.toList());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment