JAVA8學習——從源碼角度深入Stream流(學習過程)


從源代碼深入Stream /

學習的時候,官方文檔是最重要的.

及其重要的內容我們不僅要知道stream用,要知道為什么這么用,還要知道底層是怎么去實現的.

--個人注釋:從此看出,雖然新的jdk版本對開發人員提供了很大的遍歷,但是從底層角度來說,實現確實是非常復雜的.
--對外提供很簡單的接口使用. (一定是框架給封裝到底層了,所以你才用着簡單.)

遇到問題,能夠從底層深入解決問題.

學習一門技術的時候,先學會用,然后去挖掘深層次的內容(底層代碼和運作方式).

引入:Example.

public class StudentTest1 {
    public static void main(String[] args) {
        Student student1 = new Student("zhangsan", 80);
        Student student2 = new Student("lisi", 90);
        Student student3 = new Student("wangwu", 100);
        Student student4 = new Student("zhaoliu", 90);

        List<Student> students = Arrays.asList(student1, student2, student3, student4);

        //collect()方法深入源碼詳解
        //op1:集合轉換為stream, 然后stream轉換為List
        List<Student> students1 = students.stream().collect(Collectors.toList());
        students1.forEach(System.out::println);

        System.out.println("----------");
        System.out.println("count: "+ students.stream().collect(counting()));//Collectors類提供的counting()方法
        System.out.println("count: "+ students.stream().count()); //stream提供的方法 , 底層實現 mapToLong()->sum

        //當jdk底層提供有通用的方法和具體的實現方法,越具體的越好.
    }
}

靜態導入(直接導入指定Java類中實現的方法)

import static java.util.stream.Collectors.*;
  • collect:收集器
  • Collector是一個接口,是特別重要的接口.

Collector接口源碼解讀

題外話:雖然JDK提供了很多Collector的實現,但是很多人僅停留在使用階段.

我們這次一行一行的讀javadoc. 因為真的很重要.

/**
 * 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.
 一個可變的匯聚操作.將輸入元素累積到可變的結果容器當中.它會在所有元素都處理完畢后,將累積之后的結果轉換成一個最終的表示(這是一個可選操作).匯聚操作支持串行和並行兩種方式執行.
 
 --如 ArrayList:就是一個可變的容器.
 --支持並行操作:確保數據不會錯,線程可以並發.很難.另外並不是說並行一定比串行要快,因為並行是有額外開銷的.
 
 *
 * <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.
 可變的reduction(匯聚)操作包括:將元素累積到集合當中,使用StringBuilder將字符串給拼在一起,計算關於元素的sum,min,max or average等,計算數據透視圖計算:如根據銷售商獲取最大銷售額等.這個Collectors類,提供了大量的可變匯聚的實現.
 
 -- 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個函數組成的,可以對結果進行一個最終的轉化.
 4個方法分別是:
 1.創建一個新的接結果容器 <supplier()>  new  
 2.將新的數據元素給合並到一個結果容器中.<accumulator()>  add
 3.將兩個結果容器合並成一個.<combiner()>  + 
 4.將中間的累積類型,轉換成結果類型. <finisher()>  result
 每個方法都會返回一個函數式皆苦.
 --學習的時候,官方文檔是最重要的.
 *
 * <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.
 Collectors 還會返回這么一個集合 Characteristics#CONCURRENT. (也就是這個類中的枚舉類)
 
 *
 * <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.
 一個匯聚操作串行的實現,會創建一個唯一的一個結果容器.使用<Supplier>函數. 每一個輸入元素都會調用累積函數(accumulator())一次. 
 
 一個並行的實現,將會對輸入進行分區,分成多個區域,每一次分區都會創建一個結果容器,然后函數.累積每一個結果容器的內容區內形成一個,然后通過comtainer()給合並成一個.
 -- 解釋:
 combiner函數,假如有4個線程同時去執行,那么就會生成4個部分結果.
 結果分別是:1.2.3.4
 可能是:
 1.2 -> 5 
 5.3 -> 6
 6.4 -> 7
 這5.6.7新創建的集合,就叫做 新的結果容器
 
 也可能是:
 1.2 -> 1+2 (新的一個)
 1.3 -> 1(新的一個)
 這種新的折疊后的,叫做折疊成一個參數容器.
 
 *
 * <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.
 為了確保串行與並行獲得等價的結果.  collector(收集器)的函數必須滿足2個條件.
 1. identity: 同一性
 2.	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 invocations.
 a == combiner.apply(a, supplier.get())
 	supplier.get() ,獲取一個空的結果容器.
 	然后將a與空的結果容器容器. 保證a == (融合等式) .
 	
 	這個特性就是:同一性.
 	
 --部分累積的結果:是在流程中產生的中間結果.
 --解釋上述等式為什么成立:a是線程某一個分支得到的部分結果. 后面的是調用BiarnyOperator.apply()
 (List<String> list1,List<String> list2)->{list1.addAll(list2);return list1;}
 這個類似於之前說的: 將兩個結果集折疊到同一個容器.然后返回來第一個結果的融合.
 
 *
 * <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();//獲取結果容器 a1.
 *     accumulator.accept(a1, t1); //a1:每一次累積的中間結果, t1:流中下一個待累積的元素.
 *     accumulator.accept(a1, t2); //t1->a1, a1已經有東西. 然后 t2->t1 = r1 (也就是下一步)
 *     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>
所以要保證:無論是單線程,還是多線程(串行和並行)的結果都要是一樣的. 
這就是所謂的:結合性.
 
 --個人注釋:從此看出,雖然新的jdk版本對開發人員提供了很大的遍歷,但是從底層角度來說,實現確實是非常復雜的.
 --對外提供很簡單的接口使用.  (一定是框架給封裝到底層了,所以你才用着簡單.)
 
 *
 * <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.)
對於一個不包含無序的收集器來說, a1 和 a2是等價的. 條件:finisher.apply(a1).equals(finisher.apply(a2)
對於無序的收集器來說:這種等價性就沒有那么嚴格了,它會考慮到順序上的區別所對應的不相等性.

 *
 * <p>Libraries that implement reduction based on {@code Collector}, such as
 * {@link Stream#collect(Collector)}, must adhere to the following constraints:
 基於Collector 去實現匯聚(reduction)操作的這種庫, 必須遵守如下的約定.
 
 - 注釋:匯聚其實有多種實現.
 如Collectors中的reducting(). 
 如Stream接口中有三種reduce()重載的方法.
 
 這兩個有很大的本質的差別: (注意單線程和多線程情況下的影響.)
 reduce:要求不可變性
 Collectors收集器方式:可變的結果容器.
 
 * <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>
 			1. 傳遞給accumulate函數的參數,以及給Combiner的兩個參數,以及finisher函數的參數,
 				他們必須是 這幾個supplier, accumulator, or combiner 函數函數上一次調用的結果(泛型-T).
 
 *     <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>
 			2. 實現不應該對, 生成的  ---  結果 做任何的事情. 除了將他們再傳給下一個函數.
 			(中間不要做任何的操作,否則肯定是紊亂的.)
 			
 *     <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>
 			3.如果一個結果被傳遞給combiner或者finisher函數,相同的對象並沒有從函數里面返回,
 				那么他們再也不會被使用了.(表示已經被用完了.)
 				
 *     <li>Once a result is passed to the combiner or finisher function, it
 *     is never passed to the accumulator function again.</li>
 			4.一個函數如果被執行給了combiner或者finisher函數之后,它再也不會被accumulate函數調用了.
 				(就是說,如果被結束函數執行完了. 就不會再被中間操作了.)
 
 *     <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>
 			5. 對於非並發的收集起來說.從supplier, accumulator, or combiner任何的結果返回一定是被限定在當前的線程了. 所以可以被用在並行的操作了.
 			reduction的操作必須被確保被正確的分析了,4個線程,被分為4個區,不會相互干擾,再都執行完畢之后,再講中間容器進行融合.形成最終結果返回.
 
 *     <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.
  			6.對於並發的收集器,實現可以自由的選擇. 和上面的5相對於.
  			在累積階段不需要保持獨立性.
 
 *     A concurrent reduction should only be applied if the collector has the
 *     {@link Characteristics#UNORDERED} characteristics or if the
 *     originating data is unordered.</li>
 一個並發的,在這個時候一定會被使用; 無序的.
 	
--到此結束,重要的 概念基本上已經介紹完畢了.

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

		使用.三個參數構造的 of 方法,()
		三個參數
		1.結果容器
		2.將數據元素累積添加到結果容器
		3.返回結果容器.(此處使用TreeSet)

 *
 * (This behavior is also implemented by the predefined collector.預定義的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>
 上述:匯聚容器的實現過程.
 1.創建一個容器
 2.累加到容器
 3.返回結果容器.
 
 *
 * <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.)
 性能的好壞:取決於實際情況.
 (並行不一定比串行性能高.)
 
 *
 * <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>
 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)}:
 如果想創建一個組合的容器.
 就是之前用的groupingBy()的分類函數.如下例子.
 
 *
 * <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
 * @param <A> the mutable accumulation type of the reduction operation (often
 *            hidden as an implementation detail)
 * @param <R> the result type of the reduction operation
 * @since 1.8
 */

理解到這里,受益匪淺.

Collector接口詳解

Collector的三個泛型<T,A,R>詳解

 * @param <T> the type of input elements to the reduction operation
 * @param <A> the mutable accumulation type of the reduction operation (often
 *            hidden as an implementation detail)
 * @param <R> the result type of the reduction operatio
  • T:需要被融合操作的輸入參數的類型 (也就是流中的每一個元素的類型)
  • A:reduction操作的可變的累積的類型.(累積的集合的類型.)(中間結果容器的類型.)(返回結果容器的類型)
  • R:匯聚操作的結果類型.

supplier()

    /**
     * A function that creates and returns a new mutable result container.
     * 創建一個新的可變結果容器.返回 Supplier函數式接口.
     * @return a function which returns a new, mutable result container
     		泛型 - A : 可變容器的類型.
     */
    Supplier<A> supplier();

accumulator()

/**
 * A function that folds a value into a mutable result container.
 * 將一個新的元素數據元素折疊(累加)到一個結果容器當中. 返回值為 BiConsumer函數式接口
 * @return a function which folds a value into a mutable result container
 泛型-A:返回的中間容器的類型(結果類型)
 泛型-T:流中待處理的下一個元素的類型.(源類型)
 */
BiConsumer<A, T> accumulator();

combiner()

/**
和並行流緊密相關.
 * 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.
 * 接收兩個部分結果,然后給合並起來.將結果狀態從一個參數轉換成另一個參數,或者返回一個新的結果容器....*(有點難理解.) 返回一個組合的操作符函數接口類.
 
 -- 解釋:
 combiner函數,假如有4個線程同時去執行,那么就會生成4個部分結果.
 結果分別是:1.2.3.4
 可能是:
 1.2 -> 5 
 5.3 -> 6
 6.4 -> 7
 這5.6.7新創建的集合,就叫做 新的結果容器
 
 也可能是:
 1.2 -> 1+2 (新的一個)
 1.3 -> 1(新的一個)
 這種新的折疊后的,叫做折疊成一個參數容器.
 
 所以:combiner 是 專門用在 並行流中的.
 
 * @return a function which combines two partial results into a combined
 * result
 泛型-A: (結果容器類型.中間結果容器的類型.) TTT 
 */
BinaryOperator<A> combiner();

finisher()

/**
 * Perform the final transformation from the intermediate accumulation type
 * {@code A} to the final result type {@code 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}.
 *如果這個特性被設置值了的話,.....  返回一個Function接口類型.
 * @return a function which transforms the intermediate result to the final
 * result
 泛型-A :結果容器類型
 泛型-R : 最終要使用的類型.(最終返回的結果的類型.)
 */
Function<A, R> finisher();

枚舉類 Characteristics

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

靜態內部類 CollectorImpl

<此靜態類在Collectors類中.>

static class CollectorImpl<T, A, R> implements Collector<T, A, R> {
        private final Supplier<A> supplier;
        private final BiConsumer<A, T> accumulator;
        private final BinaryOperator<A> combiner;
        private final Function<A, R> finisher;
        private final Set<Characteristics> characteristics;

        CollectorImpl(Supplier<A> supplier,
                      BiConsumer<A, T> accumulator,
                      BinaryOperator<A> combiner,
                      Function<A,R> finisher,
                      Set<Characteristics> characteristics) {
            this.supplier = supplier;
            this.accumulator = accumulator;
            this.combiner = combiner;
            this.finisher = finisher;
            this.characteristics = characteristics;
        }

        CollectorImpl(Supplier<A> supplier,
                      BiConsumer<A, T> accumulator,
                      BinaryOperator<A> combiner,
                      Set<Characteristics> characteristics) {
            this(supplier, accumulator, combiner, castingIdentity(), characteristics);
        }

        @Override
        public BiConsumer<A, T> accumulator() {
            return accumulator;
        }

        @Override
        public Supplier<A> supplier() {
            return supplier;
        }

        @Override
        public BinaryOperator<A> combiner() {
            return combiner;
        }

        @Override
        public Function<A, R> finisher() {
            return finisher;
        }

        @Override
        public Set<Characteristics> characteristics() {
            return characteristics;
        }
    }

為什么會定義一個這么一個靜態內部類?

  1. 因為,Collectors是一個工廠,向開發者提供非常常見的那些收集器,如counting() , grouping by()....

  2. 絕大多數方法都是靜態方法.

  3. Collectors和CollectorImpl緊密相關,結合性非常密切.從設計角度,直接放在一個類里面.


函數式編程的最大特點:表示做什么,而不是如何做.如:toList(), counting()...

Collectors收集器注釋:

/**

		收集了常見的一些操作.
 * Implementations of {@link Collector} that implement various useful reduction
 * operations, such as accumulating elements into collections, summarizing
 * elements according to various criteria, etc.
 *
 * <p>The following are examples of using the predefined collectors to perform
 * common mutable reduction tasks:
 	使用預定義的收集器,去執行課常見的收集任務.
 
 以下案例:
 *
 * <pre>{@code
 *     // Accumulate names into a List . 將name融合到LIst中.
 *     List<String> list = people.stream().map(Person::getName).collect(Collectors.toList());
 *
 
 			融合進TreeSet
 *     // Accumulate names into a TreeSet . 
 *     Set<String> set = people.stream().map(Person::getName).collect(Collectors.toCollection(TreeSet::new));
 *
 			轉換成字符串,然后用","去分隔.
 *     // Convert elements to strings and concatenate them, separated by commas
 *     String joined = things.stream()
 *                           .map(Object::toString)
 *                           .collect(Collectors.joining(", "));
 *
 
 			計算員工的工資的總數.
 *     // Compute sum of salaries of employee
 *     int total = employees.stream()
 *                          .collect(Collectors.summingInt(Employee::getSalary)));
 
	 分組:
 
 			根據部門分組.  分類器 
 *     // Group employees by department
 *     Map<Department, List<Employee>> byDept
 *         = employees.stream()
 *                    .collect(Collectors.groupingBy(Employee::getDepartment));
 *
 
 			groupingBy的重載,處理完之后,再處理.
 *     // Compute sum of salaries by department
 *     Map<Department, Integer> totalByDept
 *         = employees.stream()
 *                    .collect(Collectors.groupingBy(Employee::getDepartment,
 *                                                   Collectors.summingInt(Employee::getSalary)));
 *
 
 		分區: partitioningBy()
 		
 *     // Partition students into passing and failing
 *     Map<Boolean, List<Student>> passingFailing =
 *         students.stream()
 *                 .collect(Collectors.partitioningBy(s -> s.getGrade() >= PASS_THRESHOLD));
 *
 * }</pre>
 *
 * @since 1.8
 */

收集器Collectors的Demo

Student student1 = new Student("zhangsan", 80);
        Student student2 = new Student("lisi", 90);
        Student student3 = new Student("wangwu", 100);
        Student student4 = new Student("zhaoliu", 90);
        Student student5 = new Student("zhaoliu", 90);

        List<Student> students = Arrays.asList(student1, student2, student3, student4,student5);

        //collect()方法深入源碼詳解
        //op1:集合轉換為stream, 然后stream轉換為List
        List<Student> students1 = students.stream().collect(Collectors.toList());
        students1.forEach(System.out::println);

        System.out.println("----------");
        System.out.println("count: "+ students.stream().collect(counting()));//Collectors類提供的counting()方法
        System.out.println("count: "+ students.stream().count()); //stream提供的方法 , 底層實現 mapToLong()->sum

        //當jdk底層提供有通用的方法和具體的實現方法,越具體的越好.

        //函數使用.
        //分數最小值
        students.stream().collect(minBy(Comparator.comparingInt(Student::getScore))).ifPresent(System.out::println);
        //分數最大值
        students.stream().collect(maxBy(Comparator.comparingInt(Student::getScore))).ifPresent(System.out::println);
        //平均值
        Double collect4 = students.stream().collect(averagingInt(Student::getScore));
        //總和
        Integer collect5 = students.stream().collect(summingInt(Student::getScore));
        //摘要信息 (分數的匯總信息.)
        students.stream().collect(summarizingInt(Student::getScore));

        System.out.println("---------");

        //字符串拼接
        String collect1 = students.stream().map(Student::getName).collect(joining());
        String collect2 = students.stream().map(Student::getName).collect(joining(","));//帶分隔符
        String collect3 = students.stream().map(Student::getName).collect(joining(",", "pre", "suf"));//帶分隔符.前綴后綴

        //分組
        //二級分組. 先根據分數分組,再根據名字分組.
        Map<Integer, Map<String, List<Student>>> collect = 
                students.stream().collect(groupingBy(Student::getScore, groupingBy(Student::getName)));
        System.out.println(collect);
        System.out.println("---------");

        //分區
        //根據分數分區
        Map<Boolean, List<Student>> collect6 = students.stream().collect(partitioningBy(student -> student.getScore() > 80));
        System.out.println(collect6);
        System.out.println("---------");

        //先分區80, 再分區90
        Map<Boolean, Map<Boolean, List<Student>>> collect7 = students.stream().collect(partitioningBy(student -> student.getScore() > 80, partitioningBy(student -> student.getScore() > 90)));
        System.out.println(collect7);
        System.out.println("---------");

        //可以看出,Collectors是可以聚合的.
        //先分區,再分組.... 先分區,再求和.... 先分組,再求平均值...  先分組,再進行各種計算...
        Map<Boolean, Long> collect8 = students.stream().collect(partitioningBy(student -> student.getScore() > 80, counting()));
        System.out.println(collect8);
        System.out.println("---------");

        //collectingAndThen() 這個方法. 先求最小值,然后再get返回值,一定是有值的.
        Map<String, Student> collect9 =
                students.stream().collect(groupingBy(Student::getName,
                        collectingAndThen(minBy(Comparator.comparingInt(Student::getScore)), Optional::get)));
        System.out.println(collect9);
        


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM