2.1 用集合创建流
因为集合继承或实现了java.util.Collection接口,而Collection接口定义了stream()和parallelStream()方法,所以可通过集合的stream() 和parallelStream()方法创建流。
// 创建集合
List list = Arrays.asList(“张三”, “李四”, “王五”);
// 创建一个串行流
Stream stream = list.stream();
// 创建一个并行流
Stream parallelStream = list.parallelStream();
1
2
3
4
5
6
2.2 用数组创建流
使用java.util.Arrays.stream(T[] array)方法用数组创建流。
// 创建数组
String[] persons = {“张三”, “李四”, “王五”};
// 创建一个串行流
Stream stream = Arrays.stream(persons);
// 创建一个并行流
Stream parallelStream = Arrays.stream(persons).parallel();
1
2
3
4
5
6
2.3 Stream静态方法
使用Stream的静态方法生成Stream,例如of()、iterate()、generate()等。
Stream stream2 = Stream.of(“张三”, “李四”, “王五”);
// 输出5个奇数 1 3 5 7 9
Stream stream3 = Stream.iterate(1, x -> x + 2).limit(5);
stream3.forEach(System.out::println);
// 生成2个UUID
Stream stream4 = Stream.generate(UUID::randomUUID).limit(2);
stream4.forEach(System.out::println);
1
2
3
4
5
6
7
8
9
三、Stream 使用案例
以下所有案例会基于学生数据,学生类,以及测试数据如下:
package com.nobody;
/**
-
@Description 学生类
-
@Author Mr.nobody
-
@Date 2021/1/17
扫描二维码关注公众号,回复: 12163721 查看本文章 -
@Version 1.0
*/
public class Student {
// 主键
private String id;
// 姓名
private String name;
// 年龄
private int age;
// 性别
private String sex;
// 成绩
private double score;public Student(String id, String name, int age, String sex, double score) {
this.id = id;
this.name = name;
this.age = age;
this.sex = sex;
this.score = score;
}
// 省略get和set方法,toString方法,如若测试需自行添加
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
List students = new ArrayList<>(16);
students.add(new Student(“1”, “张三”, 18, “male”, 88));
students.add(new Student(“2”, “李四”, 17, “male”, 60));
students.add(new Student(“3”, “王五”, 18, “male”, 100));
students.add(new Student(“4”, “赵六”, 20, “male”, 10));
students.add(new Student(“5”, “董七”, 14, “female”, 95));
students.add(new Student(“6”, “幺八”, 21, “male”, 55));
students.add(new Student(“7”, “老九”, 20, “female”, 66));
students.add(new Student(“8”, “小明”, 18, “male”, 100));
students.add(new Student(“9”, “小红”, 22, “female”, 95));
students.add(new Student(“10”, “小张”, 25, “male”, 90));
1
2
3
4
5
6
7
8
9
10
11
3.1 遍历 forEach
students.stream().forEach(System.out::println);
// 输出结果
Student{id=‘1’, name=‘张三’, age=18, sex=male, score=88.0}
Student{id=‘2’, name=‘李四’, age=17, sex=male, score=60.0}
Student{id=‘3’, name=‘王五’, age=18, sex=male, score=100.0}
Student{id=‘4’, name=‘赵六’, age=20, sex=male, score=10.0}
Student{id=‘5’, name=‘董七’, age=14, sex=female, score=95.0}
Student{id=‘6’, name=‘幺八’, age=21, sex=male, score=55.0}
Student{id=‘7’, name=‘老九’, age=20, sex=female, score=66.0}
Student{id=‘8’, name=‘小明’, age=18, sex=male, score=100.0}
Student{id=‘9’, name=‘小红’, age=22, sex=female, score=95.0}
Student{id=‘10’, name=‘小张’, age=25, sex=male, score=90.0}
1
2
3
4
5
6
7
8
9
10
11
12
13
3.2 过滤 filter
// 过滤出成绩100分的学生
List students1 =
students.stream().filter(student -> student.getScore() == 100).collect(Collectors.toList());
students1.forEach(System.out::println);
// 输出结果
Student{id=‘3’, name=‘王五’, age=18, sex=male, score=100.0}
Student{id=‘8’, name=‘小明’, age=18, sex=male, score=100.0}
1
2
3
4
5
6
7
8
3.3 查找 findFirst,findAny
一般filter和find搭配使用,即从过滤符合条件的数据中,获得一个数据。
// 串行流,匹配第一个
Optional studentOptional =
students.stream().filter(student -> student.getAge() >= 20).findFirst();
if (studentOptional.isPresent()) {
Student student = studentOptional.get();
System.out.println(student);
}
// 上面输出语句可简写如下
// studentOptional.ifPresent(System.out::println);
// 并行流,匹配任意一个,findAny一般用于并行流
Optional studentOptiona2 =
students.parallelStream().filter(student -> student.getAge() >= 20).findAny();
studentOptiona2.ifPresent(System.out::println);
// 输出结果
Student{id=‘4’, name=‘赵六’, age=20, sex=male, score=10.0}
Student{id=‘7’, name=‘老九’, age=20