diff --git a/collection-pipeline/README.md b/collection-pipeline/README.md index 3599a6d5d..ddeeddc60 100644 --- a/collection-pipeline/README.md +++ b/collection-pipeline/README.md @@ -26,4 +26,5 @@ Use the Collection Pipeline pattern when ## Credits * [Function composition and the Collection Pipeline pattern](https://www.ibm.com/developerworks/library/j-java8idioms2/index.html) -* [Martin Fowler](https://martinfowler.com/articles/collection-pipeline/) \ No newline at end of file +* [Martin Fowler](https://martinfowler.com/articles/collection-pipeline/) +* Java8 Streams (https://docs.oracle.com/javase/8/docs/api/java/util/stream/package-summary.html) \ No newline at end of file diff --git a/collection-pipeline/src/main/java/com/iluwatar/collectionpipeline/App.java b/collection-pipeline/src/main/java/com/iluwatar/collectionpipeline/App.java index 109d60b88..a36ce2ec1 100644 --- a/collection-pipeline/src/main/java/com/iluwatar/collectionpipeline/App.java +++ b/collection-pipeline/src/main/java/com/iluwatar/collectionpipeline/App.java @@ -22,7 +22,9 @@ */ package com.iluwatar.collectionpipeline; +import java.util.Arrays; import java.util.List; +import java.util.Map; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -49,12 +51,26 @@ public class App { */ public static void main(String[] args) { - List cars = Iterating.createCars(); + List cars = CarFactory.createCars(); - List modelsImperative = ImperativeProgramming.getModelsAfter2000UsingFor(cars); + List modelsImperative = ImperativeProgramming.getModelsAfter2000(cars); LOGGER.info(modelsImperative.toString()); - List modelsFunctional = FunctionalProgramming.getModelsAfter2000UsingPipeline(cars); + List modelsFunctional = FunctionalProgramming.getModelsAfter2000(cars); LOGGER.info(modelsFunctional.toString()); + + Map> groupingByCategoryImperative = ImperativeProgramming.getGroupingOfCarsByCategory(cars); + LOGGER.info(groupingByCategoryImperative.toString()); + + Map> groupingByCategoryFunctional = FunctionalProgramming.getGroupingOfCarsByCategory(cars); + LOGGER.info(groupingByCategoryFunctional.toString()); + + Person john = new Person(cars); + + List sedansOwnedImperative = ImperativeProgramming.getSedanCarsOwnedSortedByDate(Arrays.asList(john)); + LOGGER.info(sedansOwnedImperative.toString()); + + List sedansOwnedFunctional = FunctionalProgramming.getSedanCarsOwnedSortedByDate(Arrays.asList(john)); + LOGGER.info(sedansOwnedFunctional.toString()); } } diff --git a/collection-pipeline/src/main/java/com/iluwatar/collectionpipeline/Car.java b/collection-pipeline/src/main/java/com/iluwatar/collectionpipeline/Car.java index be113c7cd..aec7ea0e1 100644 --- a/collection-pipeline/src/main/java/com/iluwatar/collectionpipeline/Car.java +++ b/collection-pipeline/src/main/java/com/iluwatar/collectionpipeline/Car.java @@ -29,17 +29,19 @@ public class Car { private String make; private String model; private int year; + private String category; /** * Constructor to create an instance of car. - * @param theMake the make of the car - * @param theModel the model of the car + * @param make the make of the car + * @param model the model of the car * @param yearOfMake the year of built of the car */ - public Car(String theMake, String theModel, int yearOfMake) { - make = theMake; - model = theModel; - year = yearOfMake; + public Car(String make, String model, int yearOfMake, String category) { + this.make = make; + this.model = model; + this.year = yearOfMake; + this.category = category; } public String getMake() { @@ -53,4 +55,8 @@ public class Car { public int getYear() { return year; } + + public String getCategory() { + return category; + } } \ No newline at end of file diff --git a/collection-pipeline/src/main/java/com/iluwatar/collectionpipeline/Iterating.java b/collection-pipeline/src/main/java/com/iluwatar/collectionpipeline/CarFactory.java similarity index 79% rename from collection-pipeline/src/main/java/com/iluwatar/collectionpipeline/Iterating.java rename to collection-pipeline/src/main/java/com/iluwatar/collectionpipeline/CarFactory.java index 4293603cb..ad1127d25 100644 --- a/collection-pipeline/src/main/java/com/iluwatar/collectionpipeline/Iterating.java +++ b/collection-pipeline/src/main/java/com/iluwatar/collectionpipeline/CarFactory.java @@ -28,8 +28,8 @@ import java.util.List; /** * A factory class to create a collection of {@link Car} instances. */ -public class Iterating { - private Iterating() { +public class CarFactory { + private CarFactory() { } /** @@ -37,10 +37,10 @@ public class Iterating { * @return {@link List} of {@link Car} */ public static List createCars() { - return Arrays.asList(new Car("Jeep", "Wrangler", 2011), new Car("Jeep", "Comanche", 1990), - new Car("Dodge", "Avenger", 2010), - new Car("Buick", "Cascada", 2016), - new Car("Ford", "Focus", 2012), - new Car("Chevrolet", "Geo Metro", 1992)); + return Arrays.asList(new Car("Jeep", "Wrangler", 2011, "Jeep"), new Car("Jeep", "Comanche", 1990, "Jeep"), + new Car("Dodge", "Avenger", 2010, "Sedan"), + new Car("Buick", "Cascada", 2016, "Convertible"), + new Car("Ford", "Focus", 2012, "Sedan"), + new Car("Chevrolet", "Geo Metro", 1992, "Convertible")); } } \ No newline at end of file diff --git a/collection-pipeline/src/main/java/com/iluwatar/collectionpipeline/FunctionalProgramming.java b/collection-pipeline/src/main/java/com/iluwatar/collectionpipeline/FunctionalProgramming.java index 7d9834bc0..c48ee6f96 100644 --- a/collection-pipeline/src/main/java/com/iluwatar/collectionpipeline/FunctionalProgramming.java +++ b/collection-pipeline/src/main/java/com/iluwatar/collectionpipeline/FunctionalProgramming.java @@ -24,6 +24,7 @@ package com.iluwatar.collectionpipeline; import java.util.Comparator; import java.util.List; +import java.util.Map; import java.util.stream.Collectors; /** @@ -53,9 +54,32 @@ public class FunctionalProgramming { * @param cars {@link List} of {@link Car} to be used for filtering * @return {@link List} of {@link String} representing models built after year 2000 */ - public static List getModelsAfter2000UsingPipeline(List cars) { + public static List getModelsAfter2000(List cars) { return cars.stream().filter(car -> car.getYear() > 2000) .sorted(Comparator.comparing(Car::getYear)) .map(Car::getModel).collect(Collectors.toList()); } + + /** + * Method to group cars by category using groupingBy + * + * @param cars {@link List} of {@link Car} to be used for grouping + * @return {@link Map} of {@link String} and {@link List} of {@link Car} with category + * as key and cars belonging to that category as value + */ + public static Map> getGroupingOfCarsByCategory(List cars) { + return cars.stream().collect(Collectors.groupingBy(Car::getCategory)); + } + + /** + * Method to get all Sedan cars belonging to a group of persons sorted by year of manufacture + * + * @param persons {@link List} of {@link Person} to be used + * @return {@link List} of {@link Car} to belonging to the group + */ + public static List getSedanCarsOwnedSortedByDate(List persons) { + return persons.stream().map(Person::getCars).flatMap(List::stream) + .filter(car -> "Sedan".equals(car.getCategory())) + .sorted(Comparator.comparing(Car::getYear)).collect(Collectors.toList()); + } } diff --git a/collection-pipeline/src/main/java/com/iluwatar/collectionpipeline/ImperativeProgramming.java b/collection-pipeline/src/main/java/com/iluwatar/collectionpipeline/ImperativeProgramming.java index 092469fbd..1fac5908a 100644 --- a/collection-pipeline/src/main/java/com/iluwatar/collectionpipeline/ImperativeProgramming.java +++ b/collection-pipeline/src/main/java/com/iluwatar/collectionpipeline/ImperativeProgramming.java @@ -25,7 +25,9 @@ package com.iluwatar.collectionpipeline; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; +import java.util.HashMap; import java.util.List; +import java.util.Map; /** * Imperative-style programming to iterate over the list and get the names of @@ -55,7 +57,7 @@ public class ImperativeProgramming { * @param cars {@link List} of {@link Car} to iterate over * @return {@link List} of {@link String} of car models built after year 2000 */ - public static List getModelsAfter2000UsingFor(List cars) { + public static List getModelsAfter2000(List cars) { List carsSortedByYear = new ArrayList<>(); for (Car car : cars) { @@ -77,4 +79,54 @@ public class ImperativeProgramming { return models; } + + /** + * Method to group cars by category using for loops + * + * @param cars {@link List} of {@link Car} to be used for grouping + * @return {@link Map} of {@link String} and {@link List} of {@link Car} with + * category as key and cars belonging to that category as value + */ + public static Map> getGroupingOfCarsByCategory(List cars) { + Map> groupingByCategory = new HashMap<>(); + for (Car car: cars) { + if (groupingByCategory.containsKey(car.getCategory())) { + groupingByCategory.get(car.getCategory()).add(car); + } else { + List categoryCars = new ArrayList<>(); + categoryCars.add(car); + groupingByCategory.put(car.getCategory(), categoryCars); + } + } + return groupingByCategory; + } + + /** + * Method to get all Sedan cars belonging to a group of persons sorted by year of manufacture using for loops + * + * @param persons {@link List} of {@link Person} to be used + * @return {@link List} of {@link Car} to belonging to the group + */ + public static List getSedanCarsOwnedSortedByDate(List persons) { + List cars = new ArrayList<>(); + for (Person person: persons) { + cars.addAll(person.getCars()); + } + + List sedanCars = new ArrayList<>(); + for (Car car: cars) { + if ("Sedan".equals(car.getCategory())) { + sedanCars.add(car); + } + } + + sedanCars.sort(new Comparator() { + @Override + public int compare(Car o1, Car o2) { + return o1.getYear() - o2.getYear(); + } + }); + + return sedanCars; + } } diff --git a/collection-pipeline/src/test/java/com/iluwatar/collectionpipeline/AppTest.java b/collection-pipeline/src/test/java/com/iluwatar/collectionpipeline/AppTest.java index 26f546a16..5827c34a3 100644 --- a/collection-pipeline/src/test/java/com/iluwatar/collectionpipeline/AppTest.java +++ b/collection-pipeline/src/test/java/com/iluwatar/collectionpipeline/AppTest.java @@ -26,6 +26,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import java.util.Arrays; import java.util.List; +import java.util.Map; import org.junit.jupiter.api.Test; @@ -34,17 +35,32 @@ import org.junit.jupiter.api.Test; */ public class AppTest { - private List cars = Iterating.createCars(); + private List cars = CarFactory.createCars(); @Test public void testGetModelsAfter2000UsingFor() { - List models = ImperativeProgramming.getModelsAfter2000UsingFor(cars); + List models = ImperativeProgramming.getModelsAfter2000(cars); assertEquals(models, Arrays.asList("Avenger", "Wrangler", "Focus", "Cascada")); } @Test public void testGetModelsAfter2000UsingPipeline() { - List models = FunctionalProgramming.getModelsAfter2000UsingPipeline(cars); + List models = FunctionalProgramming.getModelsAfter2000(cars); assertEquals(models, Arrays.asList("Avenger", "Wrangler", "Focus", "Cascada")); } + + @Test + public void testGetGroupingOfCarsByCategory() { + Map> modelsFunctional = FunctionalProgramming.getGroupingOfCarsByCategory(cars); + Map> modelsImperative = ImperativeProgramming.getGroupingOfCarsByCategory(cars); + assertEquals(modelsFunctional, modelsImperative); + } + + @Test + public void testGetSedanCarsOwnedSortedByDate() { + Person john = new Person(cars); + List modelsFunctional = FunctionalProgramming.getSedanCarsOwnedSortedByDate(Arrays.asList(john)); + List modelsImperative = ImperativeProgramming.getSedanCarsOwnedSortedByDate(Arrays.asList(john)); + assertEquals(modelsFunctional, modelsImperative); + } }