Stream流介绍

是一个来自数据源的元素源的元素队列并支持聚合操作

作用范围

  • 数组 Arrays.stream()
  • 单列集合Collection
  • 双列集合Map
  • **Stream.of()**(直接构造流)
  • 文件行流Files.lines()
  • 函数生成流Stream.generate() / Stream.iterate()

中间操作

  • 过滤filter
  • 映射map, flatMap
  • 排序sorted
  • 去重distinct
  • 截断/跳过limit, skip

终止操作

遍历forEach

聚合reduce

收集collect(Collectors.toList())

统计count, max, min

匹配anyMatch, allMatch, noneMatch

查找findFirst, findAny

使用场景

练习

练习1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* @program: SoftExam
* @description:
* @author: 郭寅之(Clay_Guo)
* @create: 2025/8/24 17:00
**/
class Person {
String name;
int age;
String city;

public Person(String name, int age, String city) {
this.name = name;
this.age = age;
this.city = city;
}

public String getName() {
return name;
}

public int getAge() {
return age;
}

public String getCity() {
return city;
}

@Override
public String toString() {
return name + " (" + age + ", " + city + ")";
}
}

public class StreamExe {
public static void main(String[] args) {
List<Person> people = Arrays.asList(
new Person("Alice", 23, "Beijing"),
new Person("Bob", 30, "Shanghai"),
new Person("Charlie", 28, "Beijing"),
new Person("David", 35, "Guangzhou"),
new Person("Eve", 40, "Shanghai"),
new Person("Frank", 18, "Beijing")
);

// 过滤:找出所有来自北京 (Beijing) 的人。
people.stream().filter(p -> "Beijing".equals(p.getCity())).forEach(System.out::println);

// 映射:获取所有人的名字列表。
List<String> collect = people.stream().map(Person::getName).collect(Collectors.toList());
System.out.println(collect);

// 排序:按年龄升序排列,并输出姓名。
people.stream().sorted(Comparator.comparingInt(Person::getAge)).map(Person::getName).forEach(System.out::println);

// 去重:获取所有不重复的城市。
people.stream().map(Person::getCity).distinct().forEach(System.out::println);

// 统计:统计所有人中年龄大于 25 的人数。
System.out.println(people.stream().filter(p -> p.getAge() > 25).count());

// 最大/最小:找出年龄最大的人。
System.out.println(people.stream().max(Comparator.comparingInt(Person::getAge)));
System.out.println(people.stream().min(Comparator.comparingInt(Person::getAge)));

// 聚合:计算所有人的平均年龄。
System.out.println(people.stream().mapToInt(Person::getAge).average()); // OptionalDouble[29.0]
System.out.println(people.stream().collect(Collectors.averagingInt(Person::getAge)));

// 分组:按城市分组,输出每个城市有哪些人。
Map<String, List<Person>> collect1 = people.stream().collect(Collectors.groupingBy(Person::getCity));
System.out.println(collect1);

// 拼接字符串:把所有人的名字用 , 拼接成一个字符串。
System.out.println(people.stream().map(Person::getName).collect(Collectors.joining(",")));

// 组合操作:找出来自北京、年龄大于 20 岁的人的名字,并按字母顺序输出。
List<String> collect2 = people.stream().filter(p -> "Beijing".equals(p.city) && p.age > 20).map(Person::getName).sorted().collect(Collectors.toList());
System.out.println(collect2);
}
}

在上面的练习中,从聚合计算平均数开始的使用方法有点不太熟悉。

练习2(进阶版)