Skip to content

Commit 3dea1d0

Browse files
author
amigoscode
committed
use streams for getAllElectricCars
1 parent 7454dbf commit 3dea1d0

File tree

1 file changed

+5
-16
lines changed

1 file changed

+5
-16
lines changed

src/com/amigoscode/car/CarService.java

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.util.ArrayList;
44
import java.util.Collections;
55
import java.util.List;
6+
import java.util.stream.Collectors;
67

78
public class CarService {
89

@@ -26,22 +27,10 @@ public Car getCar(String regNumber) {
2627
}
2728

2829
public List<Car> getAllElectricCars() {
29-
List<Car> cars = getAllCars();
30-
31-
if (cars.size() == 0) {
32-
return Collections.emptyList();
33-
}
34-
35-
List<Car> electricCars = new ArrayList<>();
36-
37-
for (Car car : cars) {
38-
if (car.isElectric()) {
39-
electricCars.add(car);
40-
}
41-
}
42-
43-
44-
30+
List<Car> electricCars = getAllCars()
31+
.stream()
32+
.filter(car -> car.isElectric())
33+
.collect(Collectors.toList());
4534
return electricCars;
4635
}
4736
}

0 commit comments

Comments
 (0)