forked from brianway/java-learning
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStreamOperation.java
More file actions
34 lines (30 loc) · 1.03 KB
/
Copy pathStreamOperation.java
File metadata and controls
34 lines (30 loc) · 1.03 KB
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
package com.brianway.learning.java8.streamapi;
import static com.brianway.learning.java8.streamapi.Dish.menu;
import java.util.List;
import java.util.stream.Collectors;
/**
* Created by brian on 17/2/28.
* 流操作:中间操作和终端操作
* 流的延迟性质
* 1. limit 操作,短路
* 2. filter 和 map 操作, 循环合并
*/
public class StreamOperation {
public static void main(String[] args) {
List<String> names = menu.stream()
.filter(d -> {
System.out.println("filtering " + d.getName());
return d.getCalories() > 300;
})
.map(d -> {
System.out.println("mapping " + d.getName());
return d.getName();
})
.limit(3)
.collect(Collectors.toList());
System.out.println(names);
//forEach 是一个返回 void 的终端操作
System.out.println("------forEach-------");
menu.stream().forEach(System.out::println);
}
}