Skip to content

Commit 3a582e2

Browse files
committed
[add] add examples of lambda to show usage
1 parent 8e63087 commit 3a582e2

File tree

6 files changed

+148
-5
lines changed

6 files changed

+148
-5
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232

3333
**博客目录**
3434

35-
- [javase](/blogs/javase)
35+
- [Java SE](/blogs/javase)
3636
- [java基础巩固笔记(1)-反射.md](/blogs/javase/java基础巩固笔记(1)-反射.md)
3737
- [java基础巩固笔记(2)-泛型.md](/blogs/javase/java基础巩固笔记(2)-泛型.md)
3838
- [java基础巩固笔记(3)-类加载器.md](/blogs/javase/java基础巩固笔记(3)-类加载器.md)
@@ -42,7 +42,7 @@
4242
- [java基础巩固笔记(5)-多线程之共享数据.md](/blogs/javase/java基础巩固笔记(5)-多线程之共享数据.md)
4343
- [java基础巩固笔记(5)-多线程之线程并发库.md](/blogs/javase/java基础巩固笔记(5)-多线程之线程并发库.md)
4444
- [java基础巩固笔记(6)-注解.md](/blogs/javase/java基础巩固笔记(6)-注解.md)
45-
- [javaweb](/blogs/javaweb)
45+
- [Java Web](/blogs/javaweb)
4646
- [javaweb入门笔记(1)-Tomcat.md](/blogs/javaweb/javaweb入门笔记(1)-Tomcat.md)
4747
- [javaweb入门笔记(2)-http入门.md](/blogs/javaweb/javaweb入门笔记(2)-http入门.md)
4848
- [javaweb入门笔记(3)-Servlet.md](/blogs/javaweb/javaweb入门笔记(3)-Servlet.md)
@@ -64,7 +64,7 @@
6464
* [ ] 容器类部分使用模块 java-container
6565
* [ ] IO 部分使用模块 java-io
6666
* [ ] Java 虚拟机相关部分使用模块 java-jvm
67-
* [ ] Java 8 新特性使用模块 java8
67+
* [ ] Java 8 新特性使用模块 java8(正在进行)
6868

6969

7070
-----

java8/README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
# Java 8 新特性
22

3-
《Java 8 In Action》一书
3+
阅读《Java 8 in Action》一书的简单整理
44

55
源码参考了 **java8/Java8InAction**:[https://github.com/java8/Java8InAction](https://github.com/java8/Java8InAction)
66

7+
## lambda 表达式
8+
9+
- FilteringApples: 传递代码的例子,展示方法引用和Lambda
10+
11+
12+
## steam API
13+
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.brianway.learning.java8.lambda;
2+
3+
import java.io.BufferedReader;
4+
import java.io.FileReader;
5+
import java.io.IOException;
6+
7+
/**
8+
* Lambda实践: 环绕执行模式
9+
* 1. 行为参数化
10+
* 2. 使用函数式接口来传递行为
11+
* 3. 执行一个行为
12+
* 4. 传递 Lambda
13+
*/
14+
public class ExecuteAround {
15+
16+
private static final String RESOURCE_ROOT = ExecuteAround.class
17+
.getResource("/").getPath() + "/data-lambda.txt";
18+
19+
public static void main(String... args) throws IOException {
20+
21+
// method we want to refactor to make more flexible
22+
String result = processFileLimited();
23+
System.out.println(result);
24+
25+
System.out.println("---");
26+
27+
String oneLine = processFile(BufferedReader::readLine);
28+
System.out.println(oneLine);
29+
30+
String twoLines = processFile((BufferedReader b) -> b.readLine() + b.readLine());
31+
System.out.println(twoLines);
32+
33+
}
34+
35+
public static String processFileLimited() throws IOException {
36+
try (BufferedReader br =
37+
new BufferedReader(new FileReader(RESOURCE_ROOT))) {
38+
return br.readLine();
39+
}
40+
}
41+
42+
public static String processFile(BufferedReaderProcessor p) throws IOException {
43+
try (BufferedReader br = new BufferedReader(new FileReader(RESOURCE_ROOT))) {
44+
return p.process(br);
45+
}
46+
47+
}
48+
49+
@FunctionalInterface
50+
public interface BufferedReaderProcessor {
51+
String process(BufferedReader b) throws IOException;
52+
53+
}
54+
}

java8/src/main/java/com/brianway/learning/java8/lambda/FilteringApples.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
/**
99
* Created by brian on 16/12/26.
1010
*
11-
*
11+
* 传递代码的例子
12+
* 分别使用了方法引用和Lambda(匿名函数)
1213
*/
1314
public class FilteringApples {
1415
public static void main(String[] args) {
@@ -30,6 +31,13 @@ public static void main(String[] args) {
3031

3132
}
3233

34+
/**
35+
* 根据抽象条件筛选
36+
* 将迭代集合的逻辑和要应用到集合中每个元素的行为区分开
37+
* @param inventory
38+
* @param p
39+
* @return
40+
*/
3341
public static List<Apple> filter(List<Apple> inventory, Predicate<Apple> p) {
3442
List<Apple> result = new ArrayList<>();
3543
for (Apple apple : inventory) {
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package com.brianway.learning.java8.lambda;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.List;
6+
import java.util.function.Consumer;
7+
import java.util.function.Function;
8+
import java.util.function.Predicate;
9+
10+
/**
11+
* Created by brian on 17/2/27.
12+
* Java 8 中常用函数式接口
13+
*/
14+
public class FunctionDescriptor {
15+
16+
public static void main(String[] args) {
17+
//使用 Predicate
18+
Predicate<String> nonEmptyStringPredicate = (String s) -> !s.isEmpty();
19+
List<String> listOfStrings = Arrays.asList("aaa", "ss", "");
20+
List<String> nonEmpty = filter(listOfStrings, nonEmptyStringPredicate);
21+
System.out.println("nonEmpty.size() == 2 " + (nonEmpty.size() == 2));
22+
23+
//使用 Consumer
24+
forEach(Arrays.asList(1, 2, 3, 4, 5), System.out::println);
25+
26+
//使用 Function
27+
List<Integer> lengthsOfWord = map(
28+
Arrays.asList("lambda", "in", "action"),
29+
String::length
30+
);
31+
forEach(lengthsOfWord, System.out::println);
32+
}
33+
34+
/**
35+
* 使用 Predicate
36+
*/
37+
public static <T> List<T> filter(List<T> list, Predicate<T> p) {
38+
List<T> results = new ArrayList<>();
39+
//TODO
40+
for (T s : list) {
41+
if (p.test(s)) {
42+
results.add(s);
43+
}
44+
}
45+
return results;
46+
}
47+
48+
/**
49+
* 使用 Consumer
50+
*/
51+
public static <T> void forEach(List<T> list, Consumer<T> c) {
52+
//TODO
53+
for (T i : list) {
54+
c.accept(i);
55+
}
56+
}
57+
58+
/**
59+
* 使用 Function
60+
*/
61+
public static <T, R> List<R> map(List<T> list, Function<T, R> f) {
62+
List<R> result = new ArrayList<>();
63+
//TODO
64+
for (T s : list) {
65+
result.add(f.apply(s));
66+
}
67+
return result;
68+
}
69+
}
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

0 commit comments

Comments
 (0)