jdk8是怎麼對底層完成支持的。不瞭解底層,平時用還能夠,可是遇到問題的時候就會卡在那裏。遲遲滅有解決方案。在學習一門新技術時,先學習怎麼去用,不要執着於源碼。可是隨着用的愈來愈多,你去了解底層是比較好的一種學習方法。html
有多種方法能夠實現同一個功能.什麼方式更好呢? 越具體的方法越好. 減小自動裝箱拆箱操做
/** * 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類源碼的詳解。必要的中文總結的感受挺好的。有錯誤的地方歡迎指出。嘿嘿。一塊兒慢慢成長哦。java