Java8-Stream collect

package lambda;

import java.util.Arrays;
import java.util.List;
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.stream.Collectors;

public class StreamCollectExam {
    public static void collectExam1() {
        List<String> numbers = Arrays.asList("1", "2", "3", "4", "1", "2", "7", "8");

        // [1, 2, 3, 4, 7, 8]
        SortedSet<String> uniqueSortedNumbers = numbers.stream().collect(Collectors.toCollection(TreeSet::new));
        System.out.println(uniqueSortedNumbers);

        // [1, 2, 3, 4, 1, 2, 7, 8]
        List<String> listNumbers = numbers.stream().collect(Collectors.toList());
        System.out.println(listNumbers);
    }

    /**
     * The combiner is used only in a parallel stream.
     */
    public static void collectExam2() {
        List<String> numbers = Arrays.asList("1", "2", "3", "4", "5", "6", "7", "8");
        /**
         * print result:
         in supplier, create a StringBuilder
         in peek,param=1
         in accumulator,param=1,result[before]=,result[after]=1
         in peek,param=2
         in accumulator,param=2,result[before]=1,result[after]=12
         in peek,param=3
         in accumulator,param=3,result[before]=12,result[after]=123
         in peek,param=4
         in accumulator,param=4,result[before]=123,result[after]=1234
         in peek,param=5
         in accumulator,param=5,result[before]=1234,result[after]=12345
         in peek,param=6
         in accumulator,param=6,result[before]=12345,result[after]=123456
         in peek,param=7
         in accumulator,param=7,result[before]=123456,result[after]=1234567
         in peek,param=8
         in accumulator,param=8,result[before]=1234567,result[after]=12345678
         streamNumbers=12345678
         */
        StringBuilder streamNumbers = numbers.stream().peek(StreamCollectExam::peek)
                .collect(StreamCollectExam::supplier, StreamCollectExam::accumulator, StreamCollectExam::combiner);
        System.out.println("streamNumbers=" + streamNumbers);
        System.out.println("-------------------------");

        /**
         * print result:
         in supplier, create a StringBuilder
         in peek,param=5
         in accumulator,param=5,result[before]=,result[after]=5
         in peek,param=6
         in accumulator,param=6,result[before]=5,result[after]=56
         in supplier, create a StringBuilder
         in peek,param=7
         in accumulator,param=7,result[before]=,result[after]=7
         in peek,param=8
         in accumulator,param=8,result[before]=7,result[after]=78
         in combiner,additional=78,result[before]=56,result[after]=5678
         in supplier, create a StringBuilder
         in peek,param=3
         in accumulator,param=3,result[before]=,result[after]=3
         in peek,param=4
         in accumulator,param=4,result[before]=3,result[after]=34
         in supplier, create a StringBuilder
         in peek,param=1
         in accumulator,param=1,result[before]=,result[after]=1
         in peek,param=2
         in accumulator,param=2,result[before]=1,result[after]=12
         in combiner,additional=34,result[before]=12,result[after]=1234
         in combiner,additional=5678,result[before]=1234,result[after]=12345678
         parallelStreamNumbers=12345678
         */
        /**
         *   <R> R collect(Supplier<R> supplier, BiConsumer<R,? super T> accumulator, BiConsumer<R,R> combiner)
         *      A supplier that supplies a mutable container to store the results.For a
         *                 parallel execution, this function may be called
         *                 multiple times and must return a fresh value each time
         *      An accumulator that accumulates the results into the mutable container.
         *      A combiner that combines the partial results when used in parallel.
         *
         *   First, create a supplier which returns an StringBuilder
         *   Second, create an accumulator that receives two arguments which is the container
         *          returned from the supplier(StringBuilder in this case). and the element of the stream.
         *   Finally, a combiner combines the results into one StringBuilder
         */
        StringBuilder parallelStreamNumbers = numbers.parallelStream().peek(StreamCollectExam::peek)
                .collect(StreamCollectExam::supplier, StreamCollectExam::accumulator, StreamCollectExam::combiner);
        System.out.println("parallelStreamNumbers=" + parallelStreamNumbers);

    }

    public static void peek(String param) {
        System.out.println("in peek,param=" + param);
    }

    private static StringBuilder supplier() {
        System.out.println("in supplier, create a StringBuilder");
        return new StringBuilder();
    }

    public static void accumulator(StringBuilder result, String param) {
        System.out.print("in accumulator,param=" + param + ",result[before]=" + result);
        result.append(param);
        System.out.println(",result[after]=" + result);
    }

    public static void combiner(StringBuilder result, StringBuilder additional) {
        System.out.print("in combiner,additional=" + additional + ",result[before]=" + result);
        result.append(additional);
        System.out.println(",result[after]=" + result);
    }

    public static void main(String[] args) {
        collectExam1();
        collectExam2();
    }
}
阅读更多
文章标签: java stream collect
个人分类: Java8 stream collect
上一篇Java8-Stream operation map,reduce
下一篇Java8-Stream join
想对作者说点什么? 我来说一句

sal,ename BULK COLLECT INTO

2009年01月22日 856B 下载

oracle bulk collect

2011年10月18日 23KB 下载

Java8-Stream join

java8 sream join

lf_haven lf_haven

2016-09-01 19:34:49

阅读数:337

没有更多推荐了,返回首页

关闭
关闭