Skip to content

Commit 87995be

Browse files
committed
1. lambda表达式 案例
#bysocket
1 parent 5f86f03 commit 87995be

File tree

4 files changed

+90
-0
lines changed

4 files changed

+90
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ Java核心技术学习代码兼测试案例<br>
3131
├── org.javacore.reflection // Java 反射
3232
├── org.javacore.rtti // Java RTTI
3333
├── org.javacore.thread // Java 线程
34+
=================实战JAVA 8=================
35+
├── org.javacore.lambda // lambda表达式
3436
3537
拼命更新!顶!d=====( ̄▽ ̄*)b
3638

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package org.javacore.lambda;
2+
3+
/**
4+
* Created by bysocket on 16/7/13.
5+
*/
6+
public class LambdaRunnable {
7+
static int b = 10;
8+
9+
public static void main(String[] args) {
10+
Thread thread = new Thread(() -> {
11+
b++;
12+
System.out.println(b);
13+
});
14+
thread.start();
15+
System.out.println("Done!");
16+
}
17+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package org.javacore.lambda;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
/**
7+
* List 比较器
8+
* <p/>
9+
* Created by bysocket on 16/7/12.
10+
*/
11+
public class LambdaTest01 {
12+
public static void main(String[] args) {
13+
List<Integer> list = new ArrayList<>();
14+
list.add(4);
15+
list.add(1);
16+
list.add(3);
17+
list.add(6);
18+
19+
// list.sort(new Comparator<Integer>() {
20+
// @Override
21+
// public int compare(Integer o1, Integer o2) {
22+
// return Integer.compare(o1, o2);
23+
// }
24+
// });
25+
26+
/** Lambda表达式格式:
27+
* (Type1 param1, Type2 param2, ..., TypeN paramN) -> {
28+
* statment1;
29+
* statment2;
30+
* ...
31+
* return statmentM;
32+
* }
33+
*/
34+
// list.sort(((o1, o2) -> {
35+
// return Integer.compare(o1, o2);
36+
// }));
37+
38+
// 简写Lambda表达式
39+
list.sort((o1, o2) -> Integer.compare(o1, o2));
40+
41+
System.out.println(list.toString());
42+
}
43+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package org.javacore.lambda;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
/**
7+
* Created by bysocket on 16/7/13.
8+
*/
9+
public class LambdaTest02 {
10+
public static void main(String[] args) {
11+
List<String> list = new ArrayList<>();
12+
list.add("aaa");
13+
list.add("cccc");
14+
list.add("b");
15+
list.add("eeeee");
16+
17+
/**
18+
* :: 操作符格式 => 三种情况
19+
* 对象::实例方法
20+
* 类::静态方法
21+
* 类::实例方法 对于前两种情况,方法引用就是对参数执行该方法。比如下面两种方法
22+
*/
23+
// 按字符串大小排序忽略大小写
24+
list.sort(String::compareToIgnoreCase);
25+
// 打印 list 元素
26+
list.forEach(System.out::print);
27+
}
28+
}

0 commit comments

Comments
 (0)