mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-05-29 14:19:13 +00:00
docs: Collection pipeline explanation (#2875)
* collection pipeline docs + refactoring * restore imperative programming code
This commit is contained in:
+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();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user