|
2 | 2 |
|
3 | 3 | import com.endava.internship.domain.Privilege; |
4 | 4 | import com.endava.internship.domain.User; |
5 | | -import com.endava.internship.service.UserService; |
6 | 5 |
|
| 6 | +import java.util.Arrays; |
| 7 | +import java.util.Collection; |
| 8 | +import java.util.Comparator; |
7 | 9 | import java.util.List; |
8 | 10 | import java.util.Map; |
9 | 11 | import java.util.Optional; |
10 | 12 | import java.util.function.Function; |
11 | 13 | import java.util.function.Predicate; |
| 14 | +import java.util.stream.Collectors; |
12 | 15 |
|
13 | 16 | public class UserServiceImpl implements UserService { |
14 | 17 |
|
15 | 18 | @Override |
16 | 19 | public List<String> getFirstNamesReverseSorted(List<User> users) { |
17 | | - throw new UnsupportedOperationException("Not implemented"); |
| 20 | + return users.stream(). |
| 21 | + map(User::getFirstName). |
| 22 | + sorted((name1, name2) -> -name1.compareTo(name2)). |
| 23 | + collect(Collectors.toList()); |
18 | 24 | } |
19 | 25 |
|
20 | 26 | @Override |
21 | 27 | public List<User> sortByAgeDescAndNameAsc(final List<User> users) { |
22 | | - throw new UnsupportedOperationException("Not implemented"); |
| 28 | + return users.stream(). |
| 29 | + sorted(Comparator. |
| 30 | + comparing(User::getAge, (age1, age2) -> -age1.compareTo(age2)). |
| 31 | + thenComparing(User::getFirstName) |
| 32 | + ). |
| 33 | + collect(Collectors.toList()); |
23 | 34 | } |
24 | 35 |
|
25 | 36 | @Override |
26 | 37 | public List<Privilege> getAllDistinctPrivileges(final List<User> users) { |
27 | | - throw new UnsupportedOperationException("Not implemented"); |
| 38 | + return users.stream(). |
| 39 | + map(User::getPrivileges). |
| 40 | + flatMap(Collection::stream). |
| 41 | + distinct(). |
| 42 | + collect(Collectors.toList()); |
28 | 43 | } |
29 | 44 |
|
30 | 45 | @Override |
31 | 46 | public Optional<User> getUpdateUserWithAgeHigherThan(final List<User> users, final int age) { |
32 | | - throw new UnsupportedOperationException("Not implemented"); |
| 47 | + return users.stream(). |
| 48 | + filter(user -> user.getAge() > age). |
| 49 | + findFirst(); |
33 | 50 | } |
34 | 51 |
|
35 | 52 | @Override |
36 | 53 | public Map<Integer, List<User>> groupByCountOfPrivileges(final List<User> users) { |
37 | | - throw new UnsupportedOperationException("Not implemented"); |
| 54 | + return users.stream().collect(Collectors.groupingBy(user -> user.getPrivileges().size())); |
38 | 55 | } |
39 | 56 |
|
40 | 57 | @Override |
41 | 58 | public double getAverageAgeForUsers(final List<User> users) { |
42 | | - throw new UnsupportedOperationException("Not implemented"); |
| 59 | + return users.stream().mapToDouble(User::getAge).average().orElse(-1d); |
43 | 60 | } |
44 | 61 |
|
45 | 62 | @Override |
46 | 63 | public Optional<String> getMostFrequentLastName(final List<User> users) { |
47 | | - throw new UnsupportedOperationException("Not implemented"); |
| 64 | + Map<String, Long> map = users.stream().map(User::getLastName). |
| 65 | + collect(Collectors.groupingBy(Function.identity(), Collectors.counting())); |
| 66 | + Long max = map.values().stream().max(Long::compareTo).orElse(0L); |
| 67 | + return map.values().stream().filter(val -> val.equals(max)).count() != 1 ? Optional.empty() : |
| 68 | + map.entrySet().stream().max(Map.Entry.comparingByValue()).map(Map.Entry::getKey); |
48 | 69 | } |
49 | 70 |
|
50 | 71 | @Override |
51 | 72 | public List<User> filterBy(final List<User> users, final Predicate<User>... predicates) { |
52 | | - throw new UnsupportedOperationException("Not implemented"); |
| 73 | + return users.stream(). |
| 74 | + filter( |
| 75 | + Arrays.stream(predicates).reduce(Predicate::and).orElse(x -> true) |
| 76 | + ). |
| 77 | + collect(Collectors.toList()); |
53 | 78 | } |
54 | 79 |
|
55 | 80 | @Override |
56 | 81 | public String convertTo(final List<User> users, final String delimiter, final Function<User, String> mapFun) { |
57 | | - throw new UnsupportedOperationException("Not implemented"); |
| 82 | + return users.stream().map(mapFun).reduce((res, str) -> res += delimiter + str).orElse(""); |
58 | 83 | } |
59 | 84 |
|
60 | 85 | @Override |
61 | 86 | public Map<Privilege, List<User>> groupByPrivileges(List<User> users) { |
62 | | - throw new UnsupportedOperationException("Not implemented"); |
| 87 | + return users.stream().map(User::getPrivileges).flatMap(Collection::stream). |
| 88 | + distinct(). |
| 89 | + collect(Collectors.toMap( |
| 90 | + privilege -> privilege, |
| 91 | + privilege -> users.stream().filter(user -> user.getPrivileges().contains(privilege)).collect(Collectors.toList()) |
| 92 | + ) |
| 93 | + ); |
63 | 94 | } |
64 | 95 |
|
65 | 96 | @Override |
66 | 97 | public Map<String, Long> getNumberOfLastNames(final List<User> users) { |
67 | | - throw new UnsupportedOperationException("Not implemented"); |
| 98 | + return users.stream().collect(Collectors.groupingBy(User::getLastName, Collectors.counting())); |
68 | 99 | } |
69 | 100 | } |
0 commit comments