Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 204c86c

Browse filesBrowse files
committed
📝 update stream-2 course
1 parent f979ecd commit 204c86c
Copy full SHA for 204c86c

File tree

Expand file treeCollapse file tree

13 files changed

+158
-6
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

13 files changed

+158
-6
lines changed
Open diff view settings
Collapse file

‎README.md‎

Copy file name to clipboardExpand all lines: README.md
+1-1Lines changed: 1 addition & 1 deletion
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ Java8 改变了我们思考和编码的方式,在这里你可以学习到 Java
2828
|第 6 课 | [默认方法的妙用](https://github.com/biezhi/learn-java8/blob/master/java8-default-methods/README.md) | [网易云课堂](http://study.163.com/course/courseLearn.htm?courseId=1005047049&utm_campaign=commission&utm_source=cp-400000000397038&utm_medium=share#/learn/video?lessonId=1051518175&courseId=1005047049) ¦ [哔哩哔哩](https://www.bilibili.com/video/av19287893/index_6.html#page=6) ¦ [Youtube](https://youtu.be/sAuEnkWezDM) | [default-method](https://github.com/biezhi/learn-java8/tree/master/java8-default-methods/src/main/java/io/github/biezhi/java8/defaultmethods) |
2929
|第 7 课 | [干掉空指针之 Optional](https://github.com/biezhi/learn-java8/blob/master/java8-optional/README.md) | [网易云课堂](http://study.163.com/course/courseLearn.htm?courseId=1005047049&utm_campaign=commission&utm_source=cp-400000000397038&utm_medium=share#/learn/video?lessonId=1051511464&courseId=1005047049) ¦ [哔哩哔哩](https://www.bilibili.com/video/av19287893/index_7.html#page=7) ¦ [Youtube](https://youtu.be/br4kqCXPB9A) | [optional](https://github.com/biezhi/learn-java8/tree/master/java8-default-methods/src/main/java/io/github/biezhi/java8/optional) |
3030
|第 8 课 | [理解 Stream](https://github.com/biezhi/learn-java8/blob/master/java8-stream/README.md) | [网易云课堂](http://study.163.com/course/courseLearn.htm?courseId=1005047049&utm_campaign=commission&utm_source=cp-400000000397038&utm_medium=share#/learn/video?lessonId=1051555343&courseId=1005047049) ¦ [哔哩哔哩](https://www.bilibili.com/video/av19287893/index_8.html#page=8) ¦ [Youtube](https://youtu.be/NB9mGlNMl-w) | [stream](https://github.com/biezhi/learn-java8/tree/master/java8-stream/src/main/java/io/github/biezhi/java8/stream/lesson1) |
31-
|第 9 课 | Stream API(上)| 网易云课堂 ¦ 哔哩哔哩 ¦ Youtube | |
31+
|第 9 课 | [Stream API(上)](https://github.com/biezhi/learn-java8/blob/master/java8-stream/README.md#%E4%BD%BF%E7%94%A8%E6%B5%81) | 网易云课堂 ¦ [哔哩哔哩](https://www.bilibili.com/video/av19287893/index_9.html#page=9) ¦ [Youtube](https://youtu.be/mGwFJERNzmY) | [stream](https://github.com/biezhi/learn-java8/blob/master/java8-stream/src/main/java/io/github/biezhi/java8/stream/lesson2) |
3232
|第 10 课 | Stream API(下)| 网易云课堂 ¦ 哔哩哔哩 ¦ Youtube | |
3333
|第 11 课 | 新的日期时间 API | 网易云课堂 ¦ 哔哩哔哩 ¦ Youtube | |
3434
|第 12 课 | 并发增强 | 网易云课堂 ¦ 哔哩哔哩 ¦ Youtube | |
Collapse file

‎java8-stream/src/main/java/io/github/biezhi/java8/stream/lesson2/Example1.java‎

Copy file name to clipboardExpand all lines: java8-stream/src/main/java/io/github/biezhi/java8/stream/lesson2/Example1.java
+9Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package io.github.biezhi.java8.stream.lesson2;
22

3+
import java.util.Arrays;
4+
import java.util.List;
5+
import java.util.stream.Stream;
6+
37
/**
48
* 创建流
59
* <p>
@@ -15,7 +19,12 @@
1519
public class Example1 {
1620

1721
public static void main(String[] args) {
22+
List<String> list = Arrays.asList("hello", "world");
23+
Stream<String> stream = list.stream();
24+
25+
Stream<String> stringStream = Arrays.stream(new String[]{"hello", "world"});
1826

27+
Stream<String> stream1 = Stream.of("hello", "world");
1928
}
2029

2130
}
Collapse file

‎java8-stream/src/main/java/io/github/biezhi/java8/stream/lesson2/Example2.java‎

Copy file name to clipboardExpand all lines: java8-stream/src/main/java/io/github/biezhi/java8/stream/lesson2/Example2.java
+12Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import io.github.biezhi.java8.stream.Project;
44

55
import java.util.List;
6+
import java.util.stream.Collectors;
7+
import java.util.stream.Stream;
68

79
/**
810
* 筛选元素
@@ -20,6 +22,16 @@ public class Example2 {
2022
public static void main(String[] args) {
2123
List<Project> projects = Project.buildData();
2224

25+
List<Project> collect = projects.stream()
26+
.filter(project -> project.getStars() > 1000)
27+
.collect(Collectors.toList());
28+
29+
// distinct
30+
Stream<Integer> numbers = Stream.of(1, 2, 3, 3, 2, 4);
31+
numbers.distinct().limit(3).forEach(n -> System.out.println(n));
32+
33+
System.out.println("===================");
34+
Stream.of(1, 2, 3, 3, 2, 4).skip(4).forEach(n -> System.out.println(n));
2335
}
2436

2537
}
Collapse file

‎java8-stream/src/main/java/io/github/biezhi/java8/stream/lesson2/Example3.java‎

Copy file name to clipboardExpand all lines: java8-stream/src/main/java/io/github/biezhi/java8/stream/lesson2/Example3.java
+6Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.util.Arrays;
44
import java.util.List;
5+
import java.util.stream.Collectors;
56

67
/**
78
* 映射
@@ -15,6 +16,11 @@ public class Example3 {
1516

1617
public static void main(String[] args) {
1718
List<String> words = Arrays.asList("Java 8", "Lambdas", "In", "Action");
19+
20+
words.stream()
21+
.map(word -> word.length())
22+
.collect(Collectors.toList())
23+
.forEach(i -> System.out.println(i));
1824
}
1925

2026
}
Collapse file

‎java8-stream/src/main/java/io/github/biezhi/java8/stream/lesson2/Example4.java‎

Copy file name to clipboardExpand all lines: java8-stream/src/main/java/io/github/biezhi/java8/stream/lesson2/Example4.java
+8Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.util.Arrays;
44
import java.util.List;
5+
import java.util.stream.Collectors;
56

67
/**
78
* 扁平流 flatMap
@@ -16,6 +17,13 @@ public class Example4 {
1617

1718
public static void main(String[] args) {
1819
List<String> list = Arrays.asList("I am a boy", "I love the girl", "But the girl loves another girl");
20+
21+
list.stream()
22+
.map(word -> word.split(" ")) // Stream<String>
23+
.flatMap(Arrays::stream)
24+
.distinct()
25+
.collect(Collectors.toList());
26+
1927
}
2028

2129
}
Collapse file

‎java8-stream/src/main/java/io/github/biezhi/java8/stream/lesson2/Example5.java‎

Copy file name to clipboardExpand all lines: java8-stream/src/main/java/io/github/biezhi/java8/stream/lesson2/Example5.java
+13Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,19 @@ public class Example5 {
2121
public static void main(String[] args) {
2222
List<Project> projects = Project.buildData();
2323

24+
boolean hasBiezhi = projects.stream()
25+
.anyMatch(p -> p.getAuthor().equals("biezhi"));
26+
27+
System.out.println(hasBiezhi);
28+
29+
System.out.println(projects.stream()
30+
.allMatch(p -> p.getAuthor().equals("biezhi")));
31+
32+
System.out.println(projects.stream()
33+
.noneMatch(p -> p.getAuthor().equals("biezhi")));
34+
35+
System.out.println(projects.stream().findAny().get());
36+
System.out.println(projects.stream().findFirst().get());
2437
}
2538

2639
}
Collapse file

‎java8-stream/src/main/java/io/github/biezhi/java8/stream/lesson2/Example6.java‎

Copy file name to clipboardExpand all lines: java8-stream/src/main/java/io/github/biezhi/java8/stream/lesson2/Example6.java
+6Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ public static void main(String[] args) {
2424
for (int x : numbers) {
2525
sum += x;
2626
}
27+
28+
System.out.println(sum);
29+
30+
Integer reduce = numbers.stream()
31+
.reduce(0, (a, b) -> a + b);
32+
System.out.println(reduce);
2733
}
2834

2935
}
Collapse file

‎java8-stream/src/main/java/io/github/biezhi/java8/stream/lesson2/Example7.java‎

Copy file name to clipboardExpand all lines: java8-stream/src/main/java/io/github/biezhi/java8/stream/lesson2/Example7.java
+10-1Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
package io.github.biezhi.java8.stream.lesson2;
22

3+
import io.github.biezhi.java8.stream.Project;
4+
5+
import java.util.List;
6+
import java.util.OptionalInt;
7+
38
/**
49
* 数值流
510
* <p>
@@ -11,7 +16,11 @@
1116
public class Example7 {
1217

1318
public static void main(String[] args) {
14-
19+
List<Project> projects = Project.buildData();
20+
OptionalInt max = projects.stream()
21+
.mapToInt(p -> p.getStars())
22+
.max();
23+
System.out.println(max.getAsInt());
1524
}
1625

1726
}
Collapse file

‎java8-stream/src/main/java/io/github/biezhi/java8/stream/lesson2/Quiz1.java‎

Copy file name to clipboardExpand all lines: java8-stream/src/main/java/io/github/biezhi/java8/stream/lesson2/Quiz1.java
+16-1Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package io.github.biezhi.java8.stream.lesson2;
22

3+
import lombok.AllArgsConstructor;
4+
5+
import java.util.stream.Stream;
6+
37
/**
48
* 3. 斐波纳契元组序列
59
* <p>
@@ -16,8 +20,19 @@
1620
*/
1721
public class Quiz1 {
1822

19-
public static void main(String[] args) {
23+
@AllArgsConstructor
24+
static class Tuple{
25+
int first;
26+
int second;
27+
}
2028

29+
public static void main(String[] args) {
30+
// tuple = (0, 1)
31+
// next [0] = prev tuple [1]
32+
// next [1] = prev (tuple [0] + tuple[1])
33+
Stream.iterate(new Tuple(0, 1), tuple -> new Tuple(tuple.second, tuple.first + tuple.second))
34+
.limit(20)
35+
.forEach(tuple -> System.out.println("("+ tuple.first +","+ tuple.second +")"));
2136
}
2237

2338
}
Collapse file

‎java8-stream/src/main/java/io/github/biezhi/java8/stream/lesson2/Quiz2.java‎

Copy file name to clipboardExpand all lines: java8-stream/src/main/java/io/github/biezhi/java8/stream/lesson2/Quiz2.java
+3-1Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import java.util.ArrayList;
66
import java.util.List;
7+
import java.util.stream.Collectors;
78

89
/**
910
* 1. 你将如何利用流来筛选前两个Java项目呢?
@@ -15,8 +16,9 @@
1516
public class Quiz2 {
1617

1718
public static void main(String[] args) {
18-
List<Project> projects = new ArrayList<>();
19+
List<Project> projects = Project.buildData();
1920

21+
System.out.println(projects.stream().map(Project::getName).limit(2).collect(Collectors.toList()));
2022
}
2123

2224
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.