File tree Expand file tree Collapse file tree 2 files changed +82
-0
lines changed
main/java/by/andd3dfx/collections
test/java/by/andd3dfx/collections Expand file tree Collapse file tree 2 files changed +82
-0
lines changed Original file line number Diff line number Diff line change
1
+ package by .andd3dfx .collections ;
2
+
3
+ import lombok .AllArgsConstructor ;
4
+ import lombok .Getter ;
5
+
6
+ import java .time .LocalDate ;
7
+ import java .util .Comparator ;
8
+ import java .util .List ;
9
+ import java .util .Map ;
10
+ import java .util .stream .Collectors ;
11
+
12
+ /**
13
+ * <pre>
14
+ * Необходимо создать стрим, который отфильтрует пользователей,
15
+ * у которых дата последнего логина была не позже, чем 3 дня назад,
16
+ * отобрать только уникальные имена, сгруппировать по количеству букв в имени
17
+ * и вывести список имен с наибольшим количеством букв
18
+ *
19
+ * static class User {
20
+ * private String name;
21
+ * private LocalDate lastLogin;
22
+ * }
23
+ * </pre>
24
+ */
25
+ public class FilterUsers {
26
+
27
+ public static List <String > apply (List <User > users ) {
28
+ return users .stream ()
29
+ .filter (user -> !user .lastLogin
30
+ .plusDays (3 )
31
+ .isBefore (LocalDate .now ()))
32
+ .map (User ::getName )
33
+ .distinct ()
34
+ .collect (Collectors .groupingBy (String ::length )).entrySet ().stream ()
35
+ .max (Comparator .comparingInt (Map .Entry ::getKey ))
36
+ .map (Map .Entry ::getValue )
37
+ .orElseGet (List ::of );
38
+ }
39
+
40
+ @ Getter
41
+ @ AllArgsConstructor
42
+ public static class User {
43
+ private String name ;
44
+ private LocalDate lastLogin ;
45
+ }
46
+ }
Original file line number Diff line number Diff line change
1
+ package by .andd3dfx .collections ;
2
+
3
+ import by .andd3dfx .collections .FilterUsers .User ;
4
+ import org .junit .Test ;
5
+
6
+ import java .time .LocalDate ;
7
+ import java .util .List ;
8
+
9
+ import static org .assertj .core .api .Assertions .assertThat ;
10
+
11
+ public class FilterUsersTest {
12
+
13
+ @ Test
14
+ public void apply () {
15
+ List <User > users = List .of (
16
+ new User ("Anton" , LocalDate .now ().minusDays (5 )),
17
+ new User ("Gena" , LocalDate .now ().minusDays (3 )),
18
+ new User ("John" , LocalDate .now ().minusDays (2 )),
19
+ new User ("Vladimir" , LocalDate .now ().minusDays (1 )),
20
+ new User ("Anatoliy" , LocalDate .now ().minusDays (3 ))
21
+ );
22
+
23
+ var result = FilterUsers .apply (users );
24
+
25
+ assertThat (result ).isEqualTo (List .of ("Vladimir" , "Anatoliy" ));
26
+ }
27
+
28
+ @ Test
29
+ public void applyForEmpty () {
30
+ List <User > users = List .of ();
31
+
32
+ var result = FilterUsers .apply (users );
33
+
34
+ assertThat (result ).isEmpty ();
35
+ }
36
+ }
You can’t perform that action at this time.
0 commit comments