Map<Dish.Type, List<Dish>> dishesByType =
menu.stream().collect(groupingBy(Dish::getType));
---결과---
{FISH=[prawns, salmon], OTHER=[french fires, rice, season fruit, pizza],
MEAT=[pork, beef, chicken]}
Map<Boolean, List<Dish>> partitionedMenu =
menu.Stream().collect(partitioningBy(Dish::isVegetarian));
---결과---
{false=[pork, beef, chicken, prawns, salmon],
true=[frech fries, rice, season fruit, pizza]}
분할의 장점
public interface Collector<T, A, R> {
Supplier<A> supplier();
BiConsumer<A, T> accumulator();
Function<A, R> finisher();
BinaryOperator<A> combiner();
Set<Charateristics> characteristics();
}
supplier 메서드 : 새로운 결과 컨테이너 만들기
public Supplier<List<T>> supplier() {
return () -> new ArrayList<T>();
}
// 생성자 참조
public Supplier<List<T>> supplier() {
return ArrayList::new;
}
accumulator 메서드 : 결과 컨테이너에 요소 추가하기
public BiConsumer<List<T>, T> accumulator() {
return (list, itme) -> list.add(item);
}
// 생성자 참
public BiConsumer<List<T>, T> accumulator() {
return List::add;
}
finisher 메서드 : 최종 변환값을 결과 컨테이너로 적용하기
public Function<List<T>, List<T>> finisher() {
return Function.identity();
}
Combiner 메서드 : 두 결과 컨테이너 병합
public BinaryOperator<List<T>> combiner() {
return (list1, list2) -> {
list1.addAll(list2);
return list1;
}
}
Characteristic 메서드