@@ -0,0 +1,2712 @@
+package com.winterbe.java8;%0A%0Aimport java.util.Arrays;%0Aimport java.util.List;%0A%0A/**%0A * @author Benjamin Winterberg%0A */%0Apublic class Streams11 %7B%0A%0A    static class Person %7B%0A        String name;%0A        int age;%0A%0A        Person(String name, int age) %7B%0A            this.name = name;%0A            this.age = age;%0A        %7D%0A%0A        @Override%0A        public String toString() %7B%0A            return name;%0A        %7D%0A    %7D%0A%0A    public static void main(String%5B%5D args) %7B%0A        List%3CPerson%3E persons =%0A            Arrays.asList(%0A                new Person(%22Max%22, 18),%0A                new Person(%22Peter%22, 23),%0A                new Person(%22Pamela%22, 23),%0A                new Person(%22David%22, 12));%0A%0A//        test1(persons);%0A//        test2(persons);%0A//        test3(persons);%0A//        test4(persons);%0A        test5(persons);%0A    %7D%0A%0A    private static void test1(List%3CPerson%3E persons) %7B%0A        persons%0A            .stream()%0A            .reduce((p1, p2) -%3E p1.age %3E p2.age ? p1 : p2)%0A            .ifPresent(System.out::println);    // Pamela%0A    %7D%0A%0A    private static void test2(List%3CPerson%3E persons) %7B%0A        Person result =%0A            persons%0A                .stream()%0A                .reduce(new Person(%22%22, 0), (p1, p2) -%3E %7B%0A                    p1.age += p2.age;%0A                    p1.name += p2.name;%0A                    return p1;%0A                %7D);%0A%0A        System.out.format(%22name=%25s; age=%25s%22, result.name, result.age);%0A    %7D%0A%0A    private static void test3(List%3CPerson%3E persons) %7B%0A        Integer ageSum = persons%0A            .stream()%0A            .reduce(0, (sum, p) -%3E sum += p.age, (sum1, sum2) -%3E sum1 + sum2);%0A%0A        System.out.println(ageSum);%0A    %7D%0A%0A    private static void test4(List%3CPerson%3E persons) %7B%0A        Integer ageSum = persons%0A            .stream()%0A            .reduce(0,%0A                (sum, p) -%3E %7B%0A                    System.out.format(%22accumulator: sum=%25s; person=%25s%5Cn%22, sum, p);%0A                    return sum += p.age;%0A                %7D,%0A                (sum1, sum2) -%3E %7B%0A                    System.out.format(%22combiner: sum1=%25s; sum2=%25s%5Cn%22, sum1, sum2);%0A                    return sum1 + sum2;%0A                %7D);%0A%0A        System.out.println(ageSum);%0A    %7D%0A%0A    private static void test5(List%3CPerson%3E persons) %7B%0A        Integer ageSum = persons%0A            .parallelStream()%0A            .reduce(0,%0A                (sum, p) -%3E %7B%0A                    System.out.format(%22accumulator: sum=%25s; person=%25s%5Cn%22, sum, p);%0A                    return sum += p.age;%0A                %7D,%0A                (sum1, sum2) -%3E %7B%0A                    System.out.format(%22combiner: sum1=%25s; sum2=%25s%5Cn%22, sum1, sum2);%0A                    return sum1 + sum2;%0A                %7D);%0A%0A        System.out.println(ageSum);%0A    %7D%0A%7D%0A

Add reduce examples