Skip to content

Commit 820707e

Browse files
committed
🍱 add stream example
1 parent 3c85d25 commit 820707e

4 files changed

Lines changed: 128 additions & 0 deletions

File tree

java8-stream/README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Stream
2+
3+
## 中间操作
4+
5+
| 操作 | 类型 | 返回类型 | 操作参数 | 函数描述符 |
6+
|:-----:|:--------|:-------|:-------|:-------|
7+
| `filter` | 中间 | `Stream<T>` | `Predicate<T>` | `T -> boolean` |
8+
| `map` | 中间 | `Stream<R>` | `Function<T, R>` | `T -> R` |
9+
| `limit` | 中间 | `Stream<T>` | | |
10+
| `sorted` | 中间 | `Stream<T>` | `Comparator<T>` | `(T, T) -> int` |
11+
| `distinct` | 中间 | `Stream<T>` | | |
12+
13+
## 收集操作(终端操作)
14+
15+
| 操作 | 类型 | 目的 |
16+
|:-----:|:--------|:-------|
17+
| `forEach` | 收集 | 消费流中的每个元素并对其应用 Lambda。这一操作返回 void |
18+
| `count` | 收集 | 返回流中元素的个数。这一操作返回 long |
19+
| `collect` | 收集 | 把流归约成一个集合,比如 List、Map 甚至是 Integer。 |
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package io.github.biezhi.java8.stream;
2+
3+
import lombok.Builder;
4+
import lombok.Data;
5+
6+
/**
7+
* 项目
8+
*
9+
* @author biezhi
10+
* @date 2018/2/9
11+
*/
12+
@Data
13+
@Builder
14+
public class Project {
15+
16+
/**
17+
* 项目名称
18+
*/
19+
private String name;
20+
21+
/**
22+
* 编程语言
23+
*/
24+
private String language;
25+
26+
/**
27+
* star 数
28+
*/
29+
private Integer stars;
30+
31+
/**
32+
* 描述
33+
*/
34+
private String description;
35+
36+
/**
37+
* 作者
38+
*/
39+
private String author;
40+
41+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package io.github.biezhi.java8.stream.lesson1;
2+
3+
import io.github.biezhi.java8.stream.Project;
4+
5+
import java.util.ArrayList;
6+
import java.util.Collections;
7+
import java.util.Comparator;
8+
import java.util.List;
9+
10+
/**
11+
* @author biezhi
12+
* @date 2018/2/11
13+
*/
14+
public class Java7 {
15+
16+
public static void main(String[] args) {
17+
List<Project> result = new ArrayList<>();
18+
19+
List<Project> projects = new ArrayList<>();
20+
for (Project project : projects) {
21+
if(project.getStars() > 1000){
22+
result.add(project);
23+
}
24+
}
25+
Collections.sort(projects, new Comparator<Project>() {
26+
@Override
27+
public int compare(Project o1, Project o2) {
28+
return o1.getStars().compareTo(o2.getStars());
29+
}
30+
});
31+
32+
List<String> names = new ArrayList<>();
33+
34+
for (Project project : projects) {
35+
names.add(project.getName());
36+
}
37+
}
38+
39+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package io.github.biezhi.java8.stream.lesson1;
2+
3+
import io.github.biezhi.java8.stream.Project;
4+
5+
import java.util.ArrayList;
6+
import java.util.Collections;
7+
import java.util.Comparator;
8+
import java.util.List;
9+
import java.util.stream.Collectors;
10+
11+
/**
12+
* @author biezhi
13+
* @date 2018/2/11
14+
*/
15+
public class Java8 {
16+
17+
public static void main(String[] args) {
18+
List<Project> result = new ArrayList<>();
19+
20+
List<Project> projects = new ArrayList<>();
21+
List<String> names = projects.stream()
22+
.filter(p -> p.getStars() > 1000)
23+
.map(Project::getName)
24+
.limit(3)
25+
.collect(Collectors.toList());
26+
System.out.println(names);
27+
}
28+
29+
}

0 commit comments

Comments
 (0)