Skip to content

Commit 2f01a27

Browse files
committed
第9章示例代码
1 parent b90306b commit 2f01a27

File tree

5 files changed

+111
-0
lines changed

5 files changed

+111
-0
lines changed

src/main/java/com/wbs/java8lambda/chapter8/Peek.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,7 @@ public static void main(String[] args) {
2121
.limit(3)
2222
.peek(x -> System.out.println("after limit:" + x))
2323
.collect(Collectors.toList());
24+
25+
System.out.println(result);
2426
}
2527
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.wbs.java8lambda.chapter9;
2+
3+
/**
4+
* TODO
5+
*
6+
* @author: wangbingshuai
7+
* @create: 2019-11-07 20:14
8+
**/
9+
public class Ambiguous {
10+
public static void main(String[] args) {
11+
new C().hello();
12+
}
13+
14+
private interface A {
15+
default void hello() {
16+
System.out.println("Hello from A");
17+
}
18+
}
19+
20+
private interface B {
21+
default void hello() {
22+
System.out.println("Hello from B");
23+
}
24+
}
25+
26+
private static class C implements B, A {
27+
@Override
28+
public void hello() {
29+
A.super.hello();
30+
}
31+
}
32+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.wbs.java8lambda.chapter9;
2+
3+
/**
4+
* TODO
5+
*
6+
* @author: wangbingshuai
7+
* @create: 2019-11-07 20:19
8+
**/
9+
public class Diamond {
10+
public static void main(String[] args) {
11+
new D().hello();
12+
}
13+
14+
private interface A {
15+
default void hello() {
16+
System.out.println("Hello from A");
17+
}
18+
}
19+
20+
private interface B extends A {
21+
}
22+
23+
private interface C extends A {
24+
}
25+
26+
static class D implements B, C {
27+
28+
}
29+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.wbs.java8lambda.chapter9;
2+
3+
import java.util.Arrays;
4+
import java.util.Comparator;
5+
import java.util.List;
6+
7+
/**
8+
* TODO
9+
*
10+
* @author: wangbingshuai
11+
* @create: 2019-11-08 14:49
12+
**/
13+
public class Intro {
14+
public static void main(String[] args) {
15+
List<Integer> numbers = Arrays.asList(3, 5, 1, 2, 6);
16+
numbers.sort(Comparator.naturalOrder());
17+
System.out.println(numbers);
18+
}
19+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.wbs.java8lambda.chapter9;
2+
3+
import java.util.function.Function;
4+
5+
/**
6+
* TODO
7+
*
8+
* @author: wangbingshuai
9+
* @create: 2019-11-07 20:26
10+
**/
11+
public class Letter {
12+
public static void main(String[] args) {
13+
Function<String, String> addHeader = Letter::addHeader;
14+
Function<String, String> transformationPipeline = addHeader.andThen(Letter::checkSpelling).andThen(Letter::addFooter);
15+
System.out.println(transformationPipeline.apply("C++ stay away from me!"));
16+
}
17+
18+
private static String addHeader(String text) {
19+
return "From Raoul, Mario and Alan:" + text;
20+
}
21+
22+
private static String addFooter(String text) {
23+
return text + "Kind regards";
24+
}
25+
26+
private static String checkSpelling(String text) {
27+
return text.replaceAll("C\\+\\+", "**Censored**");
28+
}
29+
}

0 commit comments

Comments
 (0)