• Java8-Stream-No.11


    import java.util.Arrays;
    import java.util.List;
    
    public class Streams11 {
    
        static class Person {
            String name;
            int age;
    
            Person(String name, int age) {
                this.name = name;
                this.age = age;
            }
    
            @Override
            public String toString() {
                return name;
            }
        }
    
        public static void main(String[] args) {
            List<Person> persons =
                Arrays.asList(
                    new Person("Max", 18),
                    new Person("Peter", 23),
                    new Person("Pamela", 23),
                    new Person("David", 12));
    
    //        test1(persons);
    //        test2(persons);
    //        test3(persons);
    //        test4(persons);
    //        test5(persons);
            test6(persons);
        }
    
        private static void test1(List<Person> persons) {
            persons
                .stream()
                .reduce((p1, p2) -> p1.age > p2.age ? p1 : p2)
                .ifPresent(System.out::println);    // Pamela
        }
    
        private static void test2(List<Person> persons) {
            Person result =
                persons
                    .stream()
                    .reduce(new Person("", 0), (p1, p2) -> {
                        p1.age += p2.age;
                        p1.name += p2.name;
                        return p1;
                    });
    
            System.out.format("name=%s; age=%s", result.name, result.age);
        }
    
        private static void test3(List<Person> persons) {
            Integer ageSum = persons
                .stream()
                .reduce(0, (sum, p) -> sum += p.age, (sum1, sum2) -> sum1 + sum2);
    
            System.out.println(ageSum);
        }
    
        private static void test4(List<Person> persons) {
            Integer ageSum = persons
                .stream()
                .reduce(0,
                    (sum, p) -> {
                        System.out.format("accumulator: sum=%s; person=%s
    ", sum, p);
                        return sum += p.age;
                    },
                    (sum1, sum2) -> {
                        System.out.format("combiner: sum1=%s; sum2=%s
    ", sum1, sum2);
                        return sum1 + sum2;
                    });
    
            System.out.println(ageSum);
        }
    
        private static void test5(List<Person> persons) {
            Integer ageSum = persons
                .parallelStream()
                .reduce(0,
                    (sum, p) -> {
                        System.out.format("accumulator: sum=%s; person=%s
    ", sum, p);
                        return sum += p.age;
                    },
                    (sum1, sum2) -> {
                        System.out.format("combiner: sum1=%s; sum2=%s
    ", sum1, sum2);
                        return sum1 + sum2;
                    });
    
            System.out.println(ageSum);
        }
    
        private static void test6(List<Person> persons) {
            Integer ageSum = persons
                .parallelStream()
                .reduce(0,
                    (sum, p) -> {
                        System.out.format("accumulator: sum=%s; person=%s; thread=%s
    ",
                            sum, p, Thread.currentThread().getName());
                        return sum += p.age;
                    },
                    (sum1, sum2) -> {
                        System.out.format("combiner: sum1=%s; sum2=%s; thread=%s
    ",
                            sum1, sum2, Thread.currentThread().getName());
                        return sum1 + sum2;
                    });
    
            System.out.println(ageSum);
        }
    }
    
  • 相关阅读:
    [GIT] warning: LF will be replaced by CRLF问题解决方法
    最近想学的工具
    如何在webstrom中配置eslint和less
    Git常用命令
    windows下nginx安装、配置与使用
    关于 addEventListener 和 handleEvent 方法
    PHP中的魔术方法总结 :__construct, __destruct , __call, __callStatic,__get, __set, __isset, __unset , __sleep, __wakeup, __toStr
    Git使用详细教程
    9个永恒的UI设计原则
    常见浏览器兼容性问题与解决方案
  • 原文地址:https://www.cnblogs.com/bilaisheng/p/10210927.html
Copyright © 2020-2023  润新知