Java8函数式编程(4)--collector(收集器)
生活随笔
收集整理的這篇文章主要介紹了
Java8函数式编程(4)--collector(收集器)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
?
?
enum Characteristics {/***指示collector是并發的。如果一個并發collector不是unordered,則只能并發應用到非排序數據源。*/CONCURRENT,/***指示collector是unordered.歸約結果不受流中項目的遍歷和累積順序的影響。*/UNORDERED,/*** 這表明finisher 方法返回的函數是一個恒等函數 ,可以被忽略,這種情況下,累加器對象將會直接用作歸約過程的最終結果。這也意味著,將累加器A不加檢查地轉換為結果R是安全的。*Indicates that the finisher function is the identity function and* can be elided. If set, it must be the case that an unchecked cast* from A to R will succeed.*/IDENTITY_FINISH}public final <R> R collect(Supplier<R> supplier,BiConsumer<R, ? super P_OUT> accumulator,BiConsumer<R, R> combiner) {//構造一個reduce,應用evaluate方法。return evaluate(ReduceOps.makeRef(supplier, accumulator, combiner));}public final <R, A> R collect(Collector<? super P_OUT, A, R> collector) {A container;if (isParallel()&& (collector.characteristics().contains(Collector.Characteristics.CONCURRENT))&& (!isOrdered() || collector.characteristics().contains(Collector.Characteristics.UNORDERED))) {container = collector.supplier().get();BiConsumer<A, ? super P_OUT> accumulator = collector.accumulator();forEach(u -> accumulator.accept(container, u));}else {//container = evaluate(ReduceOps.makeRef(collector));}return collector.characteristics().contains(Collector.Characteristics.IDENTITY_FINISH)? (R) container: collector.finisher().apply(container);} private static <I, R> Function<I, R> castingIdentity() {return i -> (R) i;}toCollection
public static <T, C extends Collection<T>>Collector<T, ?, C> toCollection(Supplier<C> collectionFactory) {return new CollectorImpl<>(collectionFactory //Supplier, Collection<T>::add //集聚器,(r1, r2) -> { r1.addAll(r2); return r1; } //combiner,CH_ID);}toList--ArrayList
public static <T>Collector<T, ?, List<T>> toList() {return new CollectorImpl<>((Supplier<List<T>>) ArrayList::new, List::add, (left, right) -> { left.addAll(right); return left; }, CH_ID);}toSet--HashSet
public static <T> Collector<T, ?, Set<T>> toSet() {return new CollectorImpl<>((Supplier<Set<T>>) HashSet::new, Set::add,(left, right) -> { left.addAll(right); return left; },CH_UNORDERED_ID);}joining
//使用StringBuilderpublic static Collector<CharSequence, ?, String> joining() {return new CollectorImpl<CharSequence, StringBuilder, String>(StringBuilder::new, StringBuilder::append,(r1, r2) -> { r1.append(r2); return r1; },StringBuilder::toString, CH_NOID);}public static Collector<CharSequence, ?, String> joining(CharSequence delimiter) {return joining(delimiter, "", "");} //使用StringJoinerpublic static Collector<CharSequence, ?, String> joining(CharSequence delimiter,CharSequence prefix,CharSequence suffix) {return new CollectorImpl<>(() -> new StringJoiner(delimiter, prefix, suffix),StringJoiner::add, StringJoiner::merge,StringJoiner::toString, CH_NOID);}reducing
public static <T> Collector<T, ?, Optional<T>> reducing(BinaryOperator<T> op) {class OptionalBox implements Consumer<T> {T value = null;boolean present = false;@Overridepublic void accept(T t) {if (present) {value = op.apply(value, t);}else {value = t;present = true;}}}return new CollectorImpl<T, OptionalBox, Optional<T>>(OptionalBox::new, OptionalBox::accept,(a, b) -> { if (b.present) a.accept(b.value); return a; },a -> Optional.ofNullable(a.value), CH_NOID);}/*identity,初始值mapper,應用都每個元素的functionop,應用到mapper的返回值。*/public static <T, U> Collector<T, ?, U> reducing(U identity,Function<? super T, ? extends U> mapper,BinaryOperator<U> op) {return new CollectorImpl<>(boxSupplier(identity), //Supplier //accumulator,(a, t) -> { a[0] = op.apply(a[0], mapper.apply(t)); }, //combiner(a, b) -> { a[0] = op.apply(a[0], b[0]); return a; },a -> a[0] //finisher。, CH_NOID);}private static <T> Supplier<T[]> boxSupplier(T identity) {return () -> (T[]) new Object[] { identity };}counting
public static <T> Collector<T, ?, Long>counting() {return reducing(0L, e -> 1L, Long::sum);}minBy
public static <T> Collector<T, ?, Optional<T>>minBy(Comparator<? super T> comparator) {return reducing(BinaryOperator.minBy(comparator));}maxBy
public static <T> Collector<T, ?, Optional<T>>maxBy(Comparator<? super T> comparator) {return reducing(BinaryOperator.maxBy(comparator));}?
?
?
?
?
?
總結
以上是生活随笔為你收集整理的Java8函数式编程(4)--collector(收集器)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java8函数式编程(4)--终止操作(
- 下一篇: Spring--总体架构