docs: Collection pipeline explanation (#2875)

* collection pipeline docs + refactoring

* restore imperative programming code
This commit is contained in:
Ilkka Seppälä
2024-03-30 13:54:59 +02:00
committed by GitHub
parent 7c1889b8e5
commit 9538c7820c
5 changed files with 148 additions and 47 deletions
@@ -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();
}
}