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 collect(Supplier supplier,
                               BiConsumer accumulator,
                               BiConsumer combiner) {
        //构造一个reduce,应用evaluate方法。
        return evaluate(ReduceOps.makeRef(supplier, accumulator, combiner));
    }

    public final  R collect(Collector collector) {
        A container;
        if (isParallel()
                && (collector.characteristics().contains(Collector.Characteristics.CONCURRENT))
                && (!isOrdered() || collector.characteristics().contains(Collector.Characteristics.UNORDERED))) {
            container = collector.supplier().get();
            BiConsumer 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  Function castingIdentity() {
        return i -> (R) i;
    }

toCollection

    public static >
    Collector toCollection(Supplier collectionFactory) {
        return new CollectorImpl<>(
        collectionFactory       //Supplier
        , Collection::add    //集聚器
        ,(r1, r2) -> { r1.addAll(r2); return r1; }  //combiner
        ,CH_ID
        );
    }

toList--ArrayList

    public static 
    Collector> toList() {
        return new CollectorImpl<>(
        (Supplier>) ArrayList::new
        , List::add
        , (left, right) -> { left.addAll(right); return left; }
        , CH_ID);
    }

toSet--HashSet

    public static      Collector> toSet() {
        return new CollectorImpl<>(
        (Supplier>) HashSet::new
        , Set::add
        ,(left, right) -> { left.addAll(right); return left; }
        ,CH_UNORDERED_ID);
    }

joining

    //使用StringBuilder
    public static Collector joining() {
        return new CollectorImpl(
                StringBuilder::new
                , StringBuilder::append
                ,(r1, r2) -> { r1.append(r2); return r1; }
                ,StringBuilder::toString
                , CH_NOID
                );
    }


    public static Collector joining(CharSequence delimiter) {
        return joining(delimiter, "", "");
    }    
    //使用StringJoiner
    public static Collector 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  Collector>     reducing(
    BinaryOperator op
    ) 
    {
        class OptionalBox implements Consumer {
            T value = null;
            boolean present = false;

            @Override
            public void accept(T t) {
                if (present) {
                    value = op.apply(value, t);
                }
                else {
                    value = t;
                    present = true;
                }
            }
        }

        return new CollectorImpl>(
                OptionalBox::new
                , OptionalBox::accept
                ,(a, b) -> { if (b.present) a.accept(b.value); return a; }
                ,a -> Optional.ofNullable(a.value), CH_NOID);
    }

   
/*
    identity,初始值
    mapper,应用都每个元素的function
    op,应用到mapper的返回值。
    */
     public static     Collector reducing(
                                U identity,
                                Function mapper,
                                BinaryOperator 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  Supplier boxSupplier(T identity) {
        return () -> (T[]) new Object[] { identity };
    }

counting

    public static  Collector
    counting() {
        return reducing(
            0L
            , e -> 1L
            , Long::sum);
    }

minBy

    public static  Collector>
    minBy(Comparator comparator) {
        return reducing(BinaryOperator.minBy(comparator));
    }

maxBy

    public static  Collector>
    maxBy(Comparator comparator) {
        return reducing(BinaryOperator.maxBy(comparator));
    }

 

 

 

 

 

 

你可能感兴趣的:(Java基础知识,Lambda表达式)