Skip to content

Commit 8fe9940

Browse files
refactoring
1 parent aa9f6ad commit 8fe9940

File tree

1 file changed

+25
-17
lines changed

1 file changed

+25
-17
lines changed

src/Application.java

+25-17
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,19 @@
33
import src.model.Address;
44
import src.model.Person;
55

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.*;
137
import java.util.function.BinaryOperator;
148
import java.util.function.Function;
159
import java.util.function.Predicate;
16-
import java.util.function.Supplier;
1710

1811
import static java.util.Optional.ofNullable;
1912
import static java.util.stream.Collectors.*;
2013
import static src.repo.PersonRepo.getPeoples;
2114

2215
public class Application {
2316

17+
private static final String STARWITHR = "r";
2418
private static final String OSMANABAD = "Osmanabad";
25-
private static final String STARTING_WITH_R = "r";
2619

2720
public static void main(String[] args) {
2821
List<Person> peoples = getPeoples();
@@ -34,6 +27,20 @@ public static void main(String[] args) {
3427
.map(name -> name.charAt(0))
3528
.orElse(null);
3629

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+
3744
Comparator<Person> compareByAge = Comparator.comparing(Person::getAge);
3845

3946
Map<Character, Person> characterOptionalMap = peoples.stream()
@@ -42,9 +49,9 @@ public static void main(String[] args) {
4249
person -> person.orElse(null))));
4350
System.out.println(characterOptionalMap);
4451

45-
4652
}
4753

54+
4855
private static List<String> getListOfNonNullCity(List<Person> peoples) {
4956
return peoples.stream()
5057
.map(Person::getAddress)
@@ -71,7 +78,7 @@ private static void grpByAgeThenGetNameAndFilterNameStartWithR(List<Person> peop
7178
System.out.println(peoples.stream()
7279
.collect(groupingBy(Person::getAge,
7380
mapping(Person::getFname,
74-
filtering(name->name.startsWith(STARTING_WITH_R),
81+
filtering(name->name.startsWith(STARWITHR),
7582
toList())))));
7683
}
7784

@@ -83,16 +90,17 @@ private static Map<Integer, Integer> groupByAgeFindCount(List<Person> peoples) {
8390

8491
private static Set<String> getListOfCity(List<Person> peoples) {
8592

93+
Function<Person, Optional<String>> getOptionalPerson = person1 -> ofNullable(person1)
94+
.flatMap(Person::getOptionalAddress)
95+
.map(Address::getCity);
8696
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());
93100
}
94101

95102
private static List<Person> getListOfPersonTryLiveInOsmanabad(List<Person> peoples) {
103+
96104
Predicate<Person> personPredicate = person1 -> ofNullable(person1)
97105
.flatMap(Person::getOptionalAddress)
98106
.map(Address::getCity)

0 commit comments

Comments
 (0)