Skip to content

Commit a2acd97

Browse files
committed
第三章示例代码
1 parent 9a6afcf commit a2acd97

File tree

8 files changed

+180
-0
lines changed

8 files changed

+180
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.wbs.java8lambda.chapter3;
2+
3+
import com.wbs.java8lambda.entity.Apple;
4+
5+
import java.util.Comparator;
6+
7+
/**
8+
* TODO
9+
*
10+
* @author: wangbingshuai
11+
* @create: 2019-10-30 15:17
12+
**/
13+
public class AppleComparator implements Comparator<Apple> {
14+
15+
@Override
16+
public int compare(Apple o1, Apple o2) {
17+
return o1.getWeight().compareTo(o2.getWeight());
18+
}
19+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.wbs.java8lambda.chapter3;
2+
3+
import com.wbs.java8lambda.entity.Apple;
4+
5+
/**
6+
* TODO
7+
*
8+
* @author: wangbingshuai
9+
* @create: 2019-10-30 15:31
10+
**/
11+
@FunctionalInterface
12+
public interface ApplePredicate {
13+
boolean test(Apple a);
14+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.wbs.java8lambda.chapter3;
2+
3+
import java.io.BufferedReader;
4+
import java.io.IOException;
5+
6+
/**
7+
* TODO
8+
*
9+
* @author: wangbingshuai
10+
* @create: 2019-10-30 15:52
11+
**/
12+
@FunctionalInterface
13+
public interface BufferedReaderProcessor {
14+
String process(BufferedReader br) throws IOException;
15+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.wbs.java8lambda.chapter3;
2+
3+
import java.io.BufferedReader;
4+
import java.io.FileReader;
5+
import java.io.IOException;
6+
7+
/**
8+
* TODO
9+
*
10+
* @author: wangbingshuai
11+
* @create: 2019-10-30 15:42
12+
**/
13+
public class ExecuteAround {
14+
public static void main(String[] args) throws IOException {
15+
String result = processFileLimited();
16+
System.out.println(result);
17+
18+
System.out.println("---");
19+
20+
String oneLine = processFile((BufferedReader b) -> b.readLine());
21+
System.out.println(oneLine);
22+
23+
System.out.println("---");
24+
25+
String twoLine = processFile((BufferedReader br) -> br.readLine() + br.readLine());
26+
System.out.println(twoLine);
27+
}
28+
29+
private static String processFileLimited() throws IOException {
30+
// 这里的相对路径不管用的话,需要改成绝对路径
31+
try (BufferedReader br = new BufferedReader(new FileReader("lambdasinaction/chap3/data.txt"))) {
32+
return br.readLine();
33+
}
34+
}
35+
36+
private static String processFile(BufferedReaderProcessor p) throws IOException {
37+
try (BufferedReader br = new BufferedReader(new FileReader("lambdasinaction/chap3/data.txt"))) {
38+
return p.process(br);
39+
}
40+
}
41+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.wbs.java8lambda.chapter3;
2+
3+
import com.wbs.java8lambda.entity.Apple;
4+
5+
import java.util.ArrayList;
6+
import java.util.Arrays;
7+
import java.util.Comparator;
8+
import java.util.List;
9+
10+
/**
11+
* TODO
12+
*
13+
* @author: wangbingshuai
14+
* @create: 2019-10-30 15:29
15+
**/
16+
public class Lambdas {
17+
public static void main(String[] args) {
18+
Runnable r = () -> System.out.println("Hello!");
19+
r.run();
20+
21+
List<Apple> list = Arrays.asList(new Apple(80, "green"),
22+
new Apple(155, "green"),
23+
new Apple(120, "red"));
24+
List<Apple> greenApples = filter(list, (Apple a) -> "green".equals(a.getColor()));
25+
System.out.println(greenApples);
26+
27+
Comparator<Apple> c = (Apple o1, Apple o2) -> o1.getWeight().compareTo(o2.getWeight());
28+
list.sort(c);
29+
System.out.println(list);
30+
}
31+
32+
private static List<Apple> filter(List<Apple> list, ApplePredicate p) {
33+
List<Apple> result = new ArrayList<>();
34+
for (Apple apple : list) {
35+
if (p.test(apple)) {
36+
result.add(apple);
37+
}
38+
}
39+
return result;
40+
}
41+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.wbs.java8lambda.chapter3;
2+
3+
import com.wbs.java8lambda.entity.Apple;
4+
5+
import java.util.ArrayList;
6+
import java.util.Arrays;
7+
import java.util.Comparator;
8+
import java.util.List;
9+
10+
/**
11+
* TODO
12+
*
13+
* @author: wangbingshuai
14+
* @create: 2019-10-30 15:11
15+
**/
16+
public class Sorting {
17+
public static void main(String[] args) {
18+
List<Apple> list = new ArrayList<>();
19+
list.addAll(Arrays.asList(new Apple(80, "green"),
20+
new Apple(155, "green"),
21+
new Apple(120, "red")));
22+
23+
list.sort(new AppleComparator());
24+
System.out.println(list);
25+
26+
list.set(1, new Apple(30, "green"));
27+
list.sort(new Comparator<Apple>() {
28+
@Override
29+
public int compare(Apple o1, Apple o2) {
30+
return o1.getWeight().compareTo(o2.getWeight());
31+
}
32+
});
33+
System.out.println(list);
34+
35+
list.set(1, new Apple(20, "red"));
36+
list.sort((o1, o2) -> o1.getWeight().compareTo(o2.getWeight()));
37+
System.out.println(list);
38+
39+
list.set(1, new Apple(10, "red"));
40+
list.sort(Comparator.comparing(Apple::getWeight));
41+
System.out.println(list);
42+
}
43+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Java
2+
8
3+
Lambdas
4+
In
5+
Action
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
The quick brown fox jumped over the lazy dog
2+
The lazy dog jumped over the quick brown fox

0 commit comments

Comments
 (0)