23
23
24
24
package com .iluwatar .collectionpipeline ;
25
25
26
- import java .util .Collection ;
26
+ import java .util .ArrayList ;
27
27
import java .util .Collections ;
28
28
import java .util .Comparator ;
29
+ import java .util .HashMap ;
29
30
import java .util .List ;
30
31
import java .util .Map ;
31
- import java .util .stream .Collectors ;
32
32
33
33
/**
34
34
* Imperative-style programming to iterate over the list and get the names of cars made later than
@@ -57,11 +57,27 @@ private ImperativeProgramming() {
57
57
* @return {@link List} of {@link String} of car models built after year 2000
58
58
*/
59
59
public static List <String >getModelsAfter2000 (List <Car >cars ) {
60
- return cars .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 =new ArrayList <>();
61
+
62
+ for (Car car :cars ) {
63
+ if (car .getYear () >2000 ) {
64
+ carsSortedByYear .add (car );
65
+ }
66
+ }
67
+
68
+ Collections .sort (carsSortedByYear ,new Comparator <Car >() {
69
+ @ Override
70
+ public int compare (Car car1 ,Car car2 ) {
71
+ return car1 .getYear () -car2 .getYear ();
72
+ }
73
+ });
74
+
75
+ List <String >models =new ArrayList <>();
76
+ for (Car car :carsSortedByYear ) {
77
+ models .add (car .getModel ());
78
+ }
79
+
80
+ return models ;
65
81
}
66
82
67
83
/**
@@ -71,7 +87,17 @@ public static List<String> getModelsAfter2000(List<Car> cars) {
71
87
* @return {@link Map} with category as key and cars belonging to that category as value
72
88
*/
73
89
public static Map <Category ,List <Car >>getGroupingOfCarsByCategory (List <Car >cars ) {
74
- return cars .stream ().collect (Collectors .groupingBy (Car ::getCategory ));
90
+ Map <Category ,List <Car >>groupingByCategory =new HashMap <>();
91
+ for (Car car :cars ) {
92
+ if (groupingByCategory .containsKey (car .getCategory ())) {
93
+ groupingByCategory .get (car .getCategory ()).add (car );
94
+ }else {
95
+ List <Car >categoryCars =new ArrayList <>();
96
+ categoryCars .add (car );
97
+ groupingByCategory .put (car .getCategory (),categoryCars );
98
+ }
99
+ }
100
+ return groupingByCategory ;
75
101
}
76
102
77
103
/**
@@ -82,11 +108,25 @@ public static Map<Category, List<Car>> getGroupingOfCarsByCategory(List<Car> car
82
108
* @return {@link List} of {@link Car} to belonging to the group
83
109
*/
84
110
public static List <Car >getSedanCarsOwnedSortedByDate (List <Person >persons ) {
85
- return persons .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 =new ArrayList <>();
112
+ for (Person person :persons ) {
113
+ cars .addAll (person .getCars ());
114
+ }
115
+
116
+ List <Car >sedanCars =new ArrayList <>();
117
+ for (Car car :cars ) {
118
+ if (Category .SEDAN .equals (car .getCategory ())) {
119
+ sedanCars .add (car );
120
+ }
121
+ }
122
+
123
+ sedanCars .sort (new Comparator <Car >() {
124
+ @ Override
125
+ public int compare (Car o1 ,Car o2 ) {
126
+ return o1 .getYear () -o2 .getYear ();
127
+ }
128
+ });
129
+
130
+ return sedanCars ;
91
131
}
92
132
}