mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-10-14 22:17:38 +00:00
docs: Collection pipeline explanation (#2875)
* collection pipeline docs + refactoring * restore imperative programming code
This commit is contained in:
1 parent
7c1889b8e5
commit
9538c7820c
5 files changed
+148
-47
No files matched your search
@@ -22,22 +22,11 @@
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
package com.iluwatar.collectionpipeline;
|
||||
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.Getter;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
package com.iluwatar.collectionpipeline;
|
||||
|
||||
/**
|
||||
* A Car class that has the properties of make, model, year and category.
|
||||
*/
|
||||
@Getter
|
||||
@EqualsAndHashCode
|
||||
@RequiredArgsConstructor
|
||||
public class Car {
|
||||
private final String make;
|
||||
private final String model;
|
||||
private final int year;
|
||||
private final Category category;
|
||||
|
||||
}
|
||||
public record Car(String make, String model, int year, Category category) {
|
||||
}
|
||||
+7
-7
@@ -22,6 +22,7 @@
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package com.iluwatar.collectionpipeline;
|
||||
|
||||
import java.util.Comparator;
|
||||
@@ -54,9 +55,8 @@ public class FunctionalProgramming {
|
||||
* @return {@link List} of {@link String} representing models built after year 2000
|
||||
*/
|
||||
public static List<String> getModelsAfter2000(List<Car> cars) {
|
||||
return cars.stream().filter(car -> car.getYear() > 2000)
|
||||
.sorted(Comparator.comparing(Car::getYear))
|
||||
.map(Car::getModel).toList();
|
||||
return cars.stream().filter(car -> car.year() > 2000).sorted(Comparator.comparing(Car::year))
|
||||
.map(Car::model).toList();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -66,7 +66,7 @@ public class FunctionalProgramming {
|
||||
* @return {@link Map} with category as key and cars belonging to that category as value
|
||||
*/
|
||||
public static Map<Category, List<Car>> getGroupingOfCarsByCategory(List<Car> cars) {
|
||||
return cars.stream().collect(Collectors.groupingBy(Car::getCategory));
|
||||
return cars.stream().collect(Collectors.groupingBy(Car::category));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -76,8 +76,8 @@ public class FunctionalProgramming {
|
||||
* @return {@link List} of {@link Car} to belonging to the group
|
||||
*/
|
||||
public static List<Car> getSedanCarsOwnedSortedByDate(List<Person> persons) {
|
||||
return persons.stream().map(Person::getCars).flatMap(List::stream)
|
||||
.filter(car -> Category.SEDAN.equals(car.getCategory()))
|
||||
.sorted(Comparator.comparing(Car::getYear)).toList();
|
||||
return persons.stream().map(Person::cars).flatMap(List::stream)
|
||||
.filter(car -> Category.SEDAN.equals(car.category()))
|
||||
.sorted(Comparator.comparing(Car::year)).toList();
|
||||
}
|
||||
}
|
||||
+9
-9
@@ -61,7 +61,7 @@ public class ImperativeProgramming {
|
||||
List<Car> carsSortedByYear = new ArrayList<>();
|
||||
|
||||
for (Car car : cars) {
|
||||
if (car.getYear() > 2000) {
|
||||
if (car.year() > 2000) {
|
||||
carsSortedByYear.add(car);
|
||||
}
|
||||
}
|
||||
@@ -69,13 +69,13 @@ public class ImperativeProgramming {
|
||||
Collections.sort(carsSortedByYear, new Comparator<Car>() {
|
||||
@Override
|
||||
public int compare(Car car1, Car car2) {
|
||||
return car1.getYear() - car2.getYear();
|
||||
return car1.year() - car2.year();
|
||||
}
|
||||
});
|
||||
|
||||
List<String> models = new ArrayList<>();
|
||||
for (Car car : carsSortedByYear) {
|
||||
models.add(car.getModel());
|
||||
models.add(car.model());
|
||||
}
|
||||
|
||||
return models;
|
||||
@@ -90,12 +90,12 @@ public class ImperativeProgramming {
|
||||
public static Map<Category, List<Car>> getGroupingOfCarsByCategory(List<Car> cars) {
|
||||
Map<Category, List<Car>> groupingByCategory = new HashMap<>();
|
||||
for (Car car : cars) {
|
||||
if (groupingByCategory.containsKey(car.getCategory())) {
|
||||
groupingByCategory.get(car.getCategory()).add(car);
|
||||
if (groupingByCategory.containsKey(car.category())) {
|
||||
groupingByCategory.get(car.category()).add(car);
|
||||
} else {
|
||||
List<Car> categoryCars = new ArrayList<>();
|
||||
categoryCars.add(car);
|
||||
groupingByCategory.put(car.getCategory(), categoryCars);
|
||||
groupingByCategory.put(car.category(), categoryCars);
|
||||
}
|
||||
}
|
||||
return groupingByCategory;
|
||||
@@ -111,12 +111,12 @@ public class ImperativeProgramming {
|
||||
public static List<Car> getSedanCarsOwnedSortedByDate(List<Person> persons) {
|
||||
List<Car> cars = new ArrayList<>();
|
||||
for (Person person : persons) {
|
||||
cars.addAll(person.getCars());
|
||||
cars.addAll(person.cars());
|
||||
}
|
||||
|
||||
List<Car> sedanCars = new ArrayList<>();
|
||||
for (Car car : cars) {
|
||||
if (Category.SEDAN.equals(car.getCategory())) {
|
||||
if (Category.SEDAN.equals(car.category())) {
|
||||
sedanCars.add(car);
|
||||
}
|
||||
}
|
||||
@@ -124,7 +124,7 @@ public class ImperativeProgramming {
|
||||
sedanCars.sort(new Comparator<Car>() {
|
||||
@Override
|
||||
public int compare(Car o1, Car o2) {
|
||||
return o1.getYear() - o2.getYear();
|
||||
return o1.year() - o2.year();
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
@@ -25,16 +25,8 @@
|
||||
package com.iluwatar.collectionpipeline;
|
||||
|
||||
import java.util.List;
|
||||
import lombok.Getter;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
|
||||
/**
|
||||
* A Person class that has the list of cars that the person owns and use.
|
||||
*/
|
||||
@Getter
|
||||
@RequiredArgsConstructor
|
||||
public class Person {
|
||||
|
||||
private final List<Car> cars;
|
||||
|
||||
}
|
||||
public record Person(List<Car> cars) {}
|
||||
Reference in new issue
Block a user