Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit33e4a87

Browse files
authored
Fix imperative-style. (iluwatar#1180)
Signed-off-by: yichen88 <tang.yichenyves@gmail.com>
1 parent33682ad commit33e4a87

File tree

1 file changed

+54
-14
lines changed

1 file changed

+54
-14
lines changed

‎collection-pipeline/src/main/java/com/iluwatar/collectionpipeline/ImperativeProgramming.java

Lines changed: 54 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@
2323

2424
packagecom.iluwatar.collectionpipeline;
2525

26-
importjava.util.Collection;
26+
importjava.util.ArrayList;
2727
importjava.util.Collections;
2828
importjava.util.Comparator;
29+
importjava.util.HashMap;
2930
importjava.util.List;
3031
importjava.util.Map;
31-
importjava.util.stream.Collectors;
3232

3333
/**
3434
* Imperative-style programming to iterate over the list and get the names of cars made later than
@@ -57,11 +57,27 @@ private ImperativeProgramming() {
5757
* @return {@link List} of {@link String} of car models built after year 2000
5858
*/
5959
publicstaticList<String>getModelsAfter2000(List<Car>cars) {
60-
returncars.stream()
61-
.filter(car ->car.getYear() >2000)
62-
.sorted(Comparator.comparingInt(Car::getYear))
63-
.map(Car::getModel)
64-
.collect(Collectors.toList());
60+
List<Car>carsSortedByYear =newArrayList<>();
61+
62+
for (Carcar :cars) {
63+
if (car.getYear() >2000) {
64+
carsSortedByYear.add(car);
65+
}
66+
}
67+
68+
Collections.sort(carsSortedByYear,newComparator<Car>() {
69+
@Override
70+
publicintcompare(Carcar1,Carcar2) {
71+
returncar1.getYear() -car2.getYear();
72+
}
73+
});
74+
75+
List<String>models =newArrayList<>();
76+
for (Carcar :carsSortedByYear) {
77+
models.add(car.getModel());
78+
}
79+
80+
returnmodels;
6581
}
6682

6783
/**
@@ -71,7 +87,17 @@ public static List<String> getModelsAfter2000(List<Car> cars) {
7187
* @return {@link Map} with category as key and cars belonging to that category as value
7288
*/
7389
publicstaticMap<Category,List<Car>>getGroupingOfCarsByCategory(List<Car>cars) {
74-
returncars.stream().collect(Collectors.groupingBy(Car::getCategory));
90+
Map<Category,List<Car>>groupingByCategory =newHashMap<>();
91+
for (Carcar :cars) {
92+
if (groupingByCategory.containsKey(car.getCategory())) {
93+
groupingByCategory.get(car.getCategory()).add(car);
94+
}else {
95+
List<Car>categoryCars =newArrayList<>();
96+
categoryCars.add(car);
97+
groupingByCategory.put(car.getCategory(),categoryCars);
98+
}
99+
}
100+
returngroupingByCategory;
75101
}
76102

77103
/**
@@ -82,11 +108,25 @@ public static Map<Category, List<Car>> getGroupingOfCarsByCategory(List<Car> car
82108
* @return {@link List} of {@link Car} to belonging to the group
83109
*/
84110
publicstaticList<Car>getSedanCarsOwnedSortedByDate(List<Person>persons) {
85-
returnpersons.stream()
86-
.map(Person::getCars)
87-
.flatMap(Collection::stream)
88-
.filter(car ->car.getCategory() ==Category.SEDAN)
89-
.sorted(Comparator.comparingInt(Car::getYear))
90-
.collect(Collectors.toList());
111+
List<Car>cars =newArrayList<>();
112+
for (Personperson :persons) {
113+
cars.addAll(person.getCars());
114+
}
115+
116+
List<Car>sedanCars =newArrayList<>();
117+
for (Carcar :cars) {
118+
if (Category.SEDAN.equals(car.getCategory())) {
119+
sedanCars.add(car);
120+
}
121+
}
122+
123+
sedanCars.sort(newComparator<Car>() {
124+
@Override
125+
publicintcompare(Caro1,Caro2) {
126+
returno1.getYear() -o2.getYear();
127+
}
128+
});
129+
130+
returnsedanCars;
91131
}
92132
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp