자바 8 stream 의 중요 한 Collector 인터페이스
T is the generic type of the items in the stream to be collected. A is the type of the accumulator, the object on which the partial result will be accumulated during the collection process. R is the type of the object (typically, but not always, the collection) resulting from the collect operation.
T 는 수집 할 스 트림 의 일반적인 유형 입 니 다.A 는 누산기 의 유형 으로 수집 과정 에서 그 위 에 일부 결 과 를 축적 하 는 대상 이다.R 은 수집 작업 이 발생 하 는 대상 (보통 집합 되 지 않 음) 의 유형 이다.
간단 한 인 스 턴 스 사용:
import java.util.*;
import java.util.function.*;
import java.util.stream.Collector;
import static java.util.stream.Collector.Characteristics.*;
public class ToListCollector<T> implements Collector<T, List<T>, List<T>> {
@Override
public Supplier<List> supplier() {
return () -> new ArrayList();
}
@Override
public BiConsumer<List, T> accumulator() {
return (list, item) -> list.add(item);
}
@Override
public Function<List<T>, List<T>> finisher() {
return i -> i;
}
@Override
public BinaryOperator<List> combiner() {
return (list1, list2) -> {
list1.addAll(list2);
return list1;
};
}
@Override
public Set characteristics() {
return Collections.unmodifiableSet(EnumSet.of(IDENTITY_FINISH, CONCURRENT));
}
}