zoukankan      html  css  js  c++  java
  • JDK8 stream用法

    forEach举例

    public static void main(String[] args) {
        // TODO Auto-generated method stub
    
        List<Person> l = create_people();
        Stream<Person> s = l.stream();
        s.forEach(P->System.out.println(P.toString()));
    }
    
    static List<Person> create_people(){
        List<Person> people = new ArrayList<>();
        Person p = new Person(2, "furong");
        people.add(p);
        p = new Person(3, "quange");
        people.add(p);
    
        return people;
    }

    实验现象

    Person [age=2, name=furong]
    Person [age=3, name=quange]

    filter举例

    public static void main(String[] args) {
        // TODO Auto-generated method stub
    
        List<Person> l = create_people();
        Stream<Person> s = l.stream();
    
        s.filter(P->P.getAge() <= 2)
        .forEach(P->System.out.println(P.toString()));
    }

    实验现象

    Person [age=2, name=furong]

    average举例

    public static void main(String[] args) {
        // TODO Auto-generated method stub
    
        List<Person> l = create_people();
    
        System.out.println("平均年龄是 " + l.stream().mapToDouble(P->P.getAge()).average().getAsDouble());
    }

    实验现象

    平均年龄是 2.5
  • 相关阅读:
    JVM内存模型
    052-224(新增70题2018)
    052-223(新增70题2018)
    052-222(新增70题2018)
    052-221(新增70题2018)
    052-220(新增70题2018)
    052-219(新增70题2018)
    052-218(新增70题2018)
    052-217(新增70题2018)
    052-216(新增70题2018)
  • 原文地址:https://www.cnblogs.com/zhangxuechao/p/11709419.html
Copyright © 2011-2022 走看看