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
05-20 07:46