2020了你还不会Java8新特性?(四)Collector类源码分析

Collector类源码分析

jdk8是怎么对底层完成支持的。不了解底层,平时用还可以,但是遇到问题的时候就会卡在那里。迟迟灭有解决方案。在学习一门新技术时,先学习怎么去用,不要执着于源码。但是随着用的越来越多,你去了解底层是比较好的一种学习方法。

有多种方法可以实现同一个功能.什么方式更好呢? 越具体的方法越好.   减少自动装箱拆箱操作
  1. collect : 收集器
  2. Collector作为collect方法的参数。
  3. Collector作为一个接口。它是一个可变的汇聚操作,将输入元素累计到一个可变的结果容器中;它会在所有元素都处理完毕后将累计的结果作为一个最终的表示(这是一个可选操作);它支持串行与并行两种方式执行。(并不是说并行一定比串行快。)
  4. Collects本身提供了关于Collectoe的常见汇聚实现,Collectors本身实际上是一个工厂。
  5. 为了确保串行和并行的结果一致,需要进行额外的处理。必须要满足两个约束。
    identity 同一性
    associativity 结合性
  6. 同一性:对于任何一条并行线路来说 ,需要满足a == combiner.apply(a, supplier.get())。举例来说:
    (List list1,List list2 -> {list1.addAll(list2);return list1})
    结合性: 下方有举例。

Collector收集器的实现源码详解

/**
 * A <a href="package-summary.html#Reduction">mutable reduction operation</a> that
 * accumulates input elements into a mutable result container, optionally transforming
 * the accumulated result into a final representation after all input elements
 * have been processed.  Reduction operations can be performed either sequentially
 * or in parallel.

    Collector作为一个接口。它是一个可变的汇聚操作,将输入元素累计到一个可变的结果容器中;它会在所有元素都处理   完毕后将累计的结果作为一个最终的表示(这是一个可选操作);它支持串行与并行两种方式执行。(并不是说并行一定比串行快。)

 * <p>Examples of mutable reduction operations include:
 * accumulating elements into a {@code Collection}; concatenating
 * strings using a {@code StringBuilder}; computing summary information about
 * elements such as sum, min, max, or average; computing "pivot table" summaries
 * such as "maximum valued transaction by seller", etc.  The class {@link Collectors}
 * provides implementations of many common mutable reductions.

 Collects本身提供了关于Collectoe的常见汇聚实现,Collectors本身实际上是一个工厂。

 * <p>A {@code Collector} is specified by four functions that work together to
 * accumulate entries into a mutable result container, and optionally perform
 * a final transform on the result.  They are: <ul>
 *     <li>creation of a new result container ({@link #supplier()})</li>
 *     <li>incorporating a new data element into a result container ({@link #accumulator()})</li>
 *     <li>combining two result containers into one ({@link #combiner()})</li>
 *     <li>performing an optional final transform on the container ({@link #finisher()})</li>
 * </ul>

    Collector 包含了4个参数

 * <p>Collectors also have a set of characteristics, such as
 * {@link Characteristics#CONCURRENT}, that provide hints that can be used by a
 * reduction implementation to provide better performance.
 *
 * <p>A sequential implementation of a reduction using a collector would
 * create a single result container using the supplier function, and invoke the
 * accumulator function once for each input element.  A parallel implementation
 * would partition the input, create a result container for each partition,
 * accumulate the contents of each partition into a subresult for that partition,
 * and then use the combiner function to merge the subresults into a combined
 * result.

   举例说明:
  1,2, 3, 4     四个部分结果。
  1,2 -》 5
  5,3 -》 6
  6,4 -》 6  

 ### 同一性和结合性的解析: 

 * <p>To ensure that sequential and parallel executions produce equivalent
 * results, the collector functions must satisfy an <em>identity</em> and an
 * <a href="package-summary.html#Associativity">associativity</a> constraints.

 为了确保串行和并行的结果一致,需要进行额外的处理。必须要满足两个约束。
             identity 同一性
                associativity  结合性

 * <p>The identity constraint says that for any partially accumulated result,
 * combining it with an empty result container must produce an equivalent
 * result.  That is, for a partially accumulated result {@code a} that is the
 * result of any series of accumulator and combiner invocations, {@code a} must
 * be equivalent to {@code combiner.apply(a, supplier.get())}.

    同一性: 对于任何一条并行线路来说,需要满足a == combiner.apply(a, supplier.get())

 * <p>The associativity constraint says that splitting the computation must
 * produce an equivalent result.  That is, for any input elements {@code t1}
 * and {@code t2}, the results {@code r1} and {@code r2} in the computation
 * below must be equivalent:
 * <pre>{@code
 *     A a1 = supplier.get();               串行:
 *     accumulator.accept(a1, t1);  第一个参数,每次累加的中间结果。 第二个参数,下一个要处理的参数
 *     accumulator.accept(a1, t2);
 *     R r1 = finisher.apply(a1);  // result without splitting
 *
 *     A a2 = supplier.get();               并行:
 *     accumulator.accept(a2, t1);  第一个参数,每次累加的中间结果。 第二个参数,下一个要处理的参数
 *     A a3 = supplier.get();
 *     accumulator.accept(a3, t2);
 *     R r2 = finisher.apply(combiner.apply(a2, a3));  // result with splitting
 * } </pre>

 结合性: 如上例。  最终要求 r1 == r2 

 * <p>For collectors that do not have the {@code UNORDERED} characteristic,
 * two accumulated results {@code a1} and {@code a2} are equivalent if
 * {@code finisher.apply(a1).equals(finisher.apply(a2))}.  For unordered
 * collectors, equivalence is relaxed to allow for non-equality related to
 * differences in order.  (For example, an unordered collector that accumulated
 * elements to a {@code List} would consider two lists equivalent if they
 * contained the same elements, ignoring order.)

 对于无序的收集器来说,等价性就被放松了,会考虑到顺序上的区别对应的不相等性。
 两个集合中包含了相同的元素,但是忽略了顺序。这种情况下两个的集合也是等价的。

 ### collector复合与注意事项:

 * <p>Libraries that implement reduction (汇聚) based on {@code Collector}, such as
 * {@link Stream#collect(Collector)}, must adhere to the following constraints:
 * <ul>
 *     <li>The first argument passed to the accumulator function, both
 *     arguments passed to the combiner function, and the argument passed to the
 *     finisher function must be the result of a previous invocation of the
 *     result supplier, accumulator, or combiner functions.</li>

 *     <li>The implementation should not do anything with the result of any of
 *     the result supplier, accumulator, or combiner functions other than to
 *     pass them again to the accumulator, combiner, or finisher functions,
 *     or return them to the caller of the reduction operation.</li>

             具体的实现来说,不应该对中间返回的结果进行额外的操作。除了最终的返回的结果。

 *     <li>If a result is passed to the combiner or finisher
 *     function, and the same object is not returned from that function, it is
 *     never used again.</li>

            如果一个结果被传递给combiner or finisher,但是并没有返回一个你传递的对象,说明你生成了一个新的结果或者创建了新的对象。这个结果就不会再被使用了。

 *     <li>Once a result is passed to the combiner or finisher function, it
 *     is never passed to the accumulator function again.</li>

                一旦一个结果被传递给了 combiner or finisher 函数,他就不会再被传递给了accumulator函数了。

 *     <li>For non-concurrent collectors, any result returned from the result
 *     supplier, accumulator, or combiner functions must be serially
 *     thread-confined.  This enables collection to occur in parallel without
 *     the {@code Collector} needing to implement any additional synchronization.
 *     The reduction implementation must manage that the input is properly
 *     partitioned, that partitions are processed in isolation, and combining
 *     happens only after accumulation is complete.</li>

            线程和线程之间的处理都是独立的,最终结束时再进行合并。

 *     <li>For concurrent collectors, an implementation is free to (but not
 *     required to) implement reduction concurrently.  A concurrent reduction
 *     is one where the accumulator function is called concurrently from
 *     multiple threads, using the same concurrently-modifiable result container,
 *     rather than keeping the result isolated during accumulation.
 *     A concurrent reduction should only be applied if the collector has the
 *     {@link Characteristics#UNORDERED} characteristics or if the
 *     originating data is unordered.</li>

            如果不是并发收集器,4个线程会生成4个中间结果。
            是并发收集器的话,4个线程会同时调用一个结果容器。

 * </ul>
 *
 * <p>In addition to the predefined implementations in {@link Collectors}, the
 * static factory methods {@link #of(Supplier, BiConsumer, BinaryOperator, Characteristics...)}
 * can be used to construct collectors.  For example, you could create a collector
 * that accumulates widgets into a {@code TreeSet} with:
 *
 * <pre>{@code
 *     Collector<Widget, ?, TreeSet<Widget>> intoSet =
 *         Collector.of(TreeSet::new, TreeSet::add,
 *                      (left, right) -> { left.addAll(right); return left; });
 * }</pre>

 通过Collector.of(传进一个新的要操作的元素,结果容器处理的步骤,多线程处理的操作)
 将流中的每个Widget 添加到TreeSet中

 * (This behavior is also implemented by the predefined collector
 * {@link Collectors#toCollection(Supplier)}).
 *
 * @apiNote
 * Performing a reduction operation with a {@code Collector} should produce a
 * result equivalent to:
 * <pre>{@code
 *     R container = collector.supplier().get();
 *     for (T t : data)
 *         collector.accumulator().accept(container, t);
 *     return collector.finisher().apply(container);
 * }</pre>

    api的说明:  collector的finisher汇聚的实现过程。

 * <p>However, the library is free to partition the input, perform the reduction
 * on the partitions, and then use the combiner function to combine the partial
 * results to achieve a parallel reduction.  (Depending on the specific reduction
 * operation, this may perform better or worse, depending on the relative cost
 * of the accumulator and combiner functions.)

 性能取决于accumulator and combiner的代价。  也就是说 并行流 并不一定比串行流效率高。

 * <p>Collectors are designed to be <em>composed</em>; many of the methods
 * in {@link Collectors} are functions that take a collector and produce
 * a new collector.  For example, given the following collector that computes
 * the sum of the salaries of a stream of employees:
 * <pre>{@code
 *     Collector<Employee, ?, Integer> summingSalaries
 *         = Collectors.summingInt(Employee::getSalary))
 * }</pre>

  搜集器是可以组合的:  take a collector and produce a new collector.
  搜集器的实现过程。  如  员工的工资的求和。

 * If we wanted to create a collector to tabulate the sum of salaries by
 * department, we could reuse the "sum of salaries" logic using
 * {@link Collectors#groupingBy(Function, Collector)}:
 * <pre>{@code
 *     Collector<Employee, ?, Map<Department, Integer>> summingSalariesByDept
 *         = Collectors.groupingBy(Employee::getDepartment, summingSalaries);
 * }</pre>

 如果我们想要新建一个搜集器,我们可以复用之前的搜集器。
 实现过程。

 * @see Stream#collect(Collector)
 * @see Collectors
 *
 * @param <T> the type of input elements to the reduction operation
            <T>  代表 流中的每一个元素的类型。
 * @param <A> the mutable accumulation type of the reduction operation (often
 *            hidden as an implementation detail)
            <A>  代表 reduction操作的可变容器的类型。表示中间操作生成的结果的类型(如ArrayList)。
 * @param <R> the result type of the reduction operation
            <R>  代表 结果类型
 * @since 1.8
 */
public interface Collector<T, A, R>{
   /**
     * A function that creates and returns a new mutable result container.
     *  A就代表每一次返回结果的类型
     * @return a function which returns a new, mutable result container
     */
    Supplier<A> supplier();   // 提供一个结果容器

    /**
     * A function that folds a value into a mutable result container.
     *  A代表中间操作返回结果的类型。 T是下一个代操作的元素的类型。
     * @return a function which folds a value into a mutable result container
     */
    BiConsumer<A, T> accumulator();    //不断的向结果容器中添加元素。

    /**
     * A function that accepts two partial results and merges them.  The
     * combiner function may fold state from one argument into the other and
     * return that, or may return a new result container.
     *  A 中间操作返回结果的类型。
     * @return a function which combines two partial results into a combined
     * result
     */
    BinaryOperator<A> combiner();   //在多线程中  合并 部分结果。
  /**
  和并行流紧密相关的
    接收两个结果,将两个部分结果合并到一起。
  combiner函数,有4个线程同时去执行,那么就会有生成4个部分结果。

  举例说明:
  1,2, 3, 4     四个部分结果。
  1,2 -》 5
  5,3 -》 6
  6,4 -》 6
   1,2合并返回5  属于return a new result container.
   6,4合并返回6,属于The combiner function may fold state from one argument into the other and  return that。
   */

    /**
     * Perform the final transformation from the intermediate accumulation type
     * {@code A} to the final result type {@code R}.
     *R 是最终返回结果的类型。
     * <p>If the characteristic {@code IDENTITY_TRANSFORM} is
     * set, this function may be presumed to be an identity transform with an
     * unchecked cast from {@code A} to {@code R}.
     *
     * @return a function which transforms the intermediate result to the final
     * result
     */
    Function<A, R> finisher();  //  合并中间的值,给出返回值。

    /**
     * Returns a {@code Set} of {@code Collector.Characteristics} indicating
     * the characteristics of this Collector.  This set should be immutable.
     *
     * @return an immutable set of collector characteristics
     */
    Set<Characteristics> characteristics();   //特征的集合

    /**
     * Returns a new {@code Collector} described by the given {@code supplier},
     * {@code accumulator}, and {@code combiner} functions.  The resulting
     * {@code Collector} has the {@code Collector.Characteristics.IDENTITY_FINISH}
     * characteristic.
     *
     * @param supplier The supplier function for the new collector
     * @param accumulator The accumulator function for the new collector
     * @param combiner The combiner function for the new collector
     * @param characteristics The collector characteristics for the new
     *                        collector
     * @param <T> The type of input elements for the new collector
     * @param <R> The type of intermediate accumulation result, and final result,
     *           for the new collector
     * @throws NullPointerException if any argument is null
     * @return the new {@code Collector}
     */
    public static<T, R> Collector<T, R, R> of(Supplier<R> supplier,
                                              BiConsumer<R, T> accumulator,
                                              BinaryOperator<R> combiner,
                                              Characteristics... characteristics) {
        Objects.requireNonNull(supplier);
        Objects.requireNonNull(accumulator);
        Objects.requireNonNull(combiner);
        Objects.requireNonNull(characteristics);
        Set<Characteristics> cs = (characteristics.length == 0)
                                  ? Collectors.CH_ID
                                  : Collections.unmodifiableSet(EnumSet.of(Collector.Characteristics.IDENTITY_FINISH,
                                                                           characteristics));
        return new Collectors.CollectorImpl<>(supplier, accumulator, combiner, cs);
    }

    /**
     * Returns a new {@code Collector} described by the given {@code supplier},
     * {@code accumulator}, {@code combiner}, and {@code finisher} functions.
     *
     * @param supplier The supplier function for the new collector
     * @param accumulator The accumulator function for the new collector
     * @param combiner The combiner function for the new collector
     * @param finisher The finisher function for the new collector
     * @param characteristics The collector characteristics for the new
     *                        collector
     * @param <T> The type of input elements for the new collector
     * @param <A> The intermediate accumulation type of the new collector
     * @param <R> The final result type of the new collector
     * @throws NullPointerException if any argument is null
     * @return the new {@code Collector}
     */
    public static<T, A, R> Collector<T, A, R> of(Supplier<A> supplier,
                                                 BiConsumer<A, T> accumulator,
                                                 BinaryOperator<A> combiner,
                                                 Function<A, R> finisher,
                                                 Characteristics... characteristics) {
        Objects.requireNonNull(supplier);
        Objects.requireNonNull(accumulator);
        Objects.requireNonNull(combiner);
        Objects.requireNonNull(finisher);
        Objects.requireNonNull(characteristics);
        Set<Characteristics> cs = Collectors.CH_NOID;
        if (characteristics.length > 0) {
            cs = EnumSet.noneOf(Characteristics.class);
            Collections.addAll(cs, characteristics);
            cs = Collections.unmodifiableSet(cs);
        }
        return new Collectors.CollectorImpl<>(supplier, accumulator, combiner, finisher, cs);
    }

    /**
     * Characteristics indicating properties of a {@code Collector}, which can
     * be used to optimize reduction implementations.
     */
    enum Characteristics {  // 特征
        /**
         * Indicates that this collector is <em>concurrent</em>, meaning that
         * the result container can support the accumulator function being
         * called concurrently with the same result container from multiple
         * threads.
         *
         * <p>If a {@code CONCURRENT} collector is not also {@code UNORDERED},
         * then it should only be evaluated concurrently if applied to an
         * unordered data source.
         */
        CONCURRENT,

        /**
         * Indicates that the collection operation does not commit to preserving
         * the encounter order of input elements.  (This might be true if the
         * result container has no intrinsic order, such as a {@link Set}.)
         */
        UNORDERED,

        /**
         * 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
    }
}

本文是针对collect类源码的详解。必要的中文总结的感觉挺好的。有错误的地方欢迎指出。嘿嘿。一起慢慢成长哦。

原文地址:https://www.cnblogs.com/wobushitiegan/p/12150604.html

时间: 2024-11-03 22:16:32

2020了你还不会Java8新特性?(四)Collector类源码分析的相关文章

2020了你还不会Java8新特性?Lambda表达式及API(二)

lambda表达式 为什么要使用lambda表示式 在Java中无法将函数座位参数传递给一个方法,也无法返回一个函数的方法. 在js中,函数的参数是一个函数.返回值是另一个函数的情况是非常常见的.是一门经典的函数式语言. Java匿名内部类. 匿名内部类的介绍 Gradle的使用.可以完全使用maven的中央仓库. 进行安卓的开发时,gradle已经成为标配了. lambda: 匿名内部类 my_jButton.addActionListener(new ActionListener() { @

2020了你还不会Java8新特性?(五)收集器比较器用法详解及源码剖析

收集器用法详解与多级分组和分区 为什么在collectors类中定义一个静态内部类? static class CollectorImpl<T, A, R> implements Collector<T, A, R> 设计上,本身就是一个辅助类,是一个工厂.作用是给开发者提供常见的收集器实现.提供的方法都是静态方法,可以直接调用. 函数式编程最大的特点:表示做什么,而不是如何做.开发者更注重如做什么,底层实现如何做. /** * Implementations of {@link

MySQL 8 新特性之信用盘源码搭建出售降序索引实现

什么是降序索引 大家可能对索引比较熟悉,而对降序索引比较陌生,事实上降序索引是索引的子集. 我们通常使用下面的语句来创建一个索引: 信用盘源码搭建出售q-1152880099 create index idx_t1_bcd on t1(b,c,d); 上面sql的意思是在t1表中,针对b,c,d三个字段创建一个联合索引. 但是大家不知道的是,上面这个sql实际上和下面的这个sql是等价的: create index idx_t1_bcd on t1(b asc,c asc,d asc); asc

java8新特性四-默认方法

简介 Java 8 新增了接口的默认方法. 简单说,默认方法就是接口可以有实现方法,而且不需要实现类去实现其方法. 我们只需在方法名前面加个 default 关键字即可实现默认方法. 为什么要有这个特性? 首先,之前的接口是个双刃剑,好处是面向抽象而不是面向具体编程,缺陷是,当需要修改接口时候,需要修改全部实现该接口的类,目前的 java 8 之前的集合框架没有 foreach 方法,通常能想到的解决办法是在JDK里给相关的接口添加新的方法及实现.然而,对于已经发布的版本,是没法再给接口添加新方

cocos2d-x 源码分析 : EventDispatcher、EventListener、Event 源码分析 (新触摸机制,新的NotificationCenter机制)

源码版本来自3.x,转载请注明 cocos2d-x 源码分析总目录 http://blog.csdn.net/u011225840/article/details/31743129 1.继承结构 1.1 结构 不详吐槽太多,也不贴图了,贴图要审核好久好久好久好久. 从小到大,先来看下Event的结构. 1.Event--------EventTouch,EventCustom,EventMouse,EventKeyboard,EventFocus,EventAcceleration 其中,Eve

JAVA8新特性——Lamda表达式

JAVA9都要出来了,JAVA8新特性都没搞清楚,是不是有点掉队哦~ Lamda表达式,读作λ表达式,它实质属于函数式编程的概念,要理解函数式编程的产生目的,就要先理解匿名内部类. 先来看看传统的匿名内部类调用方式: interface MyInterface{ void lMethod(); } public class Main { public static void test(MyInterface myInterface){ myInterface.lMethod(); } publi

Android 使用Java8新特性之&quot;方法引用&quot;

前言 上一文:Android 使用Java8新特性之Lambda expression (附命令者模式简化) 说过lambda表达式,在android studio中的环境配置及应用.本文讲下Java8新特性之"方法引用". "方法引用",它其实可以看成lambda表达式的一种简写形式. 再回顾一下lambda表达式的应用场景:简化仅含单一抽象方法接口的调用 方法引用的4种形式 方法引用的符号形式,形如, [className | class-instance]::

Java8新特性浅析

欢迎阅读我编写的Java 8介绍.本教程将带领你一步一步地认识这门语言的新特性.通过简单明了的代码示例,你将会学习到如何使用默认接口方法,Lambda表达式,方法引用和重复注解.看完这篇教程后,你还将对最新推出的API有一定的了解,例如:流控制,函数式接口,map扩展和新的时间日期API等等. 允许在接口中有默认方法实现 Java 8 允许我们使用default关键字,为接口声明添加非抽象的方法实现.这个特性又被称为扩展方法.下面是我们的第一个例子: 1 2 3 4 5 6 7 interfac

乐字节-Java8新特性-接口默认方法之Stream流(下)

接上一篇:<Java8新特性之stream>,下面继续接着讲Stream 5.流的中间操作 常见的流的中间操作,归为以下三大类:筛选和切片流操作.元素映射操作.元素排序操作: 操作 描述 筛选和切片 filter(T -> boolean):保留 boolean 为 true 的元素 limit(long n):返回前 n 个元素 skip(long n):去除前 n 个元素 distinct():去除重复元素,这个方法是通过类的 equals 方法来判断两个元素是否相等的 映射 map