x
60
60
1
import java.util.ArrayList;
2
import java.util.List;
3
4
public class IntStreamTest1 {
5
6
public static void main(String[] args) {
7
8
List<Person> people = new ArrayList<>();
9
people.add(new Person("Alice", 30));
10
people.add(new Person("Bob", 19));
11
people.add(new Person("Ronald", 40));
12
people.add(new Person("Jimi", 27));
13
people.add(new Person("Suzan", 60));
14
people.add(new Person("Jeff", 80));
15
people.add(new Person("Maria", 52));
16
17
// min
18
int min = people.stream().mapToInt(p -> p.getAge()).min().orElse(0);
19
System.out.println(min);
20
// 19
21
22
// max
23
int max = people.stream().mapToInt(p -> p.getAge()).max().orElse(0);
24
System.out.println(max);
25
// 80
26
27
// sum
28
int sum = people.stream().mapToInt(p -> p.getAge()).sum();
29
System.out.println(sum);
30
// 308
31
32
// average
33
double ave = people.stream().mapToInt(p -> p.getAge()).average().orElse(0.0);
34
System.out.println(ave);
35
// 44.0
36
}
37
}
38
39
class Person {
40
41
private String name;
42
private Integer age;
43
44
public Person(String name, Integer age) {
45
this.name = name;
46
this.age = age;
47
}
48
49
public Integer getAge() {
50
return age;
51
}
52
53
public String getName() {
54
return name;
55
}
56
57
public String toString() {
58
return "name:" + name + " age:" + age;
59
}
60
}