-
Notifications
You must be signed in to change notification settings - Fork 8
/
Jdk9Stream.java
71 lines (64 loc) · 2.31 KB
/
Jdk9Stream.java
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
package net.codingme.feature.jdk9;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;
/**
* * @author 达西 - 公众号:未读代码
*/
public class Jdk9Stream {
/**
* takeWhile ,从头开始筛选,遇到不满足的就结束了
*
* @param args
*/
public static void main(String[] args) {
// takeWhile ,从头开始筛选,遇到不满足的就结束了
List<Integer> list1 = List.of(1, 2, 3, 4, 5);
List<Integer> listResult = list1.stream().takeWhile(x -> x < 3).collect(Collectors.toList());
System.out.println(listResult);
// takeWhile ,从头开始筛选,遇到不满足的就结束
List<Integer> list2 = List.of(1, 2, 3, 4, 3, 0);
List<Integer> listResult2 = list2.stream().takeWhile(x -> x < 3).collect(Collectors.toList());
System.out.println(listResult2);
}
/**
* dropWhile ,从头开始删除,遇到不满足的就结束了
*/
public void dropWhile() {
// dropWhile ,从头开始删除,遇到不满足的就结束了
List<Integer> list1 = List.of(1, 2, 3, 4, 5);
List<Integer> listResult = list1.stream().dropWhile(x -> x < 3).collect(Collectors.toList());
System.out.println(listResult);
// dropWhile ,从头开始删除,遇到不满足的就结束
List<Integer> list2 = List.of(1, 2, 3, 4, 3, 0);
List<Integer> listResult2 = list2.stream().dropWhile(x -> x < 3).collect(Collectors.toList());
System.out.println(listResult2);
}
/**
* 使用 ofNullable 创建支持 null 的 stream
*/
public void ofNullable() {
Stream<Integer> stream = Stream.of(1, 2, null);
stream.forEach(System.out::print);
System.out.println();
// 空指针异常
// stream = Stream.of(null);
stream = Stream.ofNullable(null);
stream.forEach(System.out::print);
}
/**
* 重载迭代器
*/
public void iterate() {
IntStream.iterate(0, x -> x < 10, x -> x + 1).forEach(System.out::print);
}
/**
* Optional 转 Stream
*/
public void optionalToStream() {
Stream<Integer> s = Optional.of(1).stream();
s.forEach(System.out::print);
}
}