3
3
import src .model .Address ;
4
4
import src .model .Person ;
5
5
6
- import java .util .Comparator ;
7
- import java .util .LinkedHashMap ;
8
- import java .util .List ;
9
- import java .util .Map ;
10
- import java .util .Objects ;
11
- import java .util .Optional ;
12
- import java .util .Set ;
6
+ import java .util .*;
13
7
import java .util .function .BinaryOperator ;
14
8
import java .util .function .Function ;
15
9
import java .util .function .Predicate ;
16
- import java .util .function .Supplier ;
17
10
18
11
import static java .util .Optional .ofNullable ;
19
12
import static java .util .stream .Collectors .*;
20
13
import static src .repo .PersonRepo .getPeoples ;
21
14
22
15
public class Application {
23
16
17
+ private static final String STARWITHR = "r" ;
24
18
private static final String OSMANABAD = "Osmanabad" ;
25
- private static final String STARTING_WITH_R = "r" ;
26
19
27
20
public static void main (String [] args ) {
28
21
List <Person > peoples = getPeoples ();
@@ -34,6 +27,20 @@ public static void main(String[] args) {
34
27
.map (name -> name .charAt (0 ))
35
28
.orElse (null );
36
29
30
+
31
+ Function <Person ,String > getCity = person ->
32
+ Optional .ofNullable (person )
33
+ .flatMap (Person ::getOptionalAddress )
34
+ .map (Address ::getCity )
35
+ .orElse (null );
36
+
37
+
38
+ List <String > collect = peoples .stream ()
39
+ .map (p -> ofNullable (p .getAddress ())
40
+ .map (Address ::getCity ))
41
+ .flatMap (Optional ::stream )
42
+ .collect (toList ());
43
+
37
44
Comparator <Person > compareByAge = Comparator .comparing (Person ::getAge );
38
45
39
46
Map <Character , Person > characterOptionalMap = peoples .stream ()
@@ -42,9 +49,9 @@ public static void main(String[] args) {
42
49
person -> person .orElse (null ))));
43
50
System .out .println (characterOptionalMap );
44
51
45
-
46
52
}
47
53
54
+
48
55
private static List <String > getListOfNonNullCity (List <Person > peoples ) {
49
56
return peoples .stream ()
50
57
.map (Person ::getAddress )
@@ -71,7 +78,7 @@ private static void grpByAgeThenGetNameAndFilterNameStartWithR(List<Person> peop
71
78
System .out .println (peoples .stream ()
72
79
.collect (groupingBy (Person ::getAge ,
73
80
mapping (Person ::getFname ,
74
- filtering (name ->name .startsWith (STARTING_WITH_R ),
81
+ filtering (name ->name .startsWith (STARWITHR ),
75
82
toList ())))));
76
83
}
77
84
@@ -83,16 +90,17 @@ private static Map<Integer, Integer> groupByAgeFindCount(List<Person> peoples) {
83
90
84
91
private static Set <String > getListOfCity (List <Person > peoples ) {
85
92
93
+ Function <Person , Optional <String >> getOptionalPerson = person1 -> ofNullable (person1 )
94
+ .flatMap (Person ::getOptionalAddress )
95
+ .map (Address ::getCity );
86
96
return peoples .stream ()
87
- .map (person -> ofNullable (person )
88
- .flatMap (Person ::getOptionalAddress )
89
- .map (Address ::getCity ))
90
- .filter (Optional ::isPresent )
91
- .map (Optional ::get )
92
- .collect (toSet ());
97
+ .map (getOptionalPerson )
98
+ .flatMap (Optional ::stream )
99
+ .collect (toSet ());
93
100
}
94
101
95
102
private static List <Person > getListOfPersonTryLiveInOsmanabad (List <Person > peoples ) {
103
+
96
104
Predicate <Person > personPredicate = person1 -> ofNullable (person1 )
97
105
.flatMap (Person ::getOptionalAddress )
98
106
.map (Address ::getCity )
0 commit comments