1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | public interface Collector<T, A, R> { /** Return a function that creates a new empty result container */ Supplier<A> supplier(); /** Return a function that incorporates an element into a container */ BiConsumer<A, T> accumulator(); /** Return a function that merges two result containers */ BinaryOperator<A> combiner(); /** Return a function that converts the intermediate result container into the final representation */ Function<A, R> finisher(); /** Special characteristics of this collector */ Set<Characteristics> characteristics(); } |
1 2 3 4 | return new CollectorImpl<>(ArrayList::new, List::add, (left, right) -> { left.addAll(right); return left; }, CH_ID); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | public class IntSummaryStatistics implements IntConsumer { private long count; private long sum; private int min = Integer.MAX_VALUE; private int max = Integer.MIN_VALUE; public void accept(int value) { ++count; sum += value; min = Math.min(min, value); max = Math.max(max, value); } public void combine(IntSummaryStatistics other) { count += other.count; sum += other.sum; min = Math.min(min, other.min); max = Math.max(max, other.max); } // plus getters for count, sum, min, max, and average } |
1 2 3 4 5 6 7 8 | public static <T> Collector<T, ?, IntSummaryStatistics> summarizingInt(ToIntFunction<? super T> mapper) { return new CollectorImpl<T, IntSummaryStatistics, IntSummaryStatistics>( IntSummaryStatistics::new, (r, t) -> r.accept(mapper.applyAsInt(t)), (l, r) -> { l.combine(r); return l; }, CH_ID); } |
欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/) | Powered by Discuz! 7.0.0 |