Skip to content

Commit bb7b56c

Browse files
Lambda表达式
1 parent 29f2399 commit bb7b56c

File tree

7 files changed

+113
-0
lines changed

7 files changed

+113
-0
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.lambda;
2+
3+
import java.util.Arrays;
4+
import java.util.List;
5+
6+
public class LambdaForeach {
7+
public static void main(String[] args) {
8+
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7);
9+
list.forEach(x-> System.out.println(x));
10+
// 也可以使用双冒号::
11+
// list.forEach(System.out::println);
12+
}
13+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.lambda;
2+
3+
import java.util.Arrays;
4+
import java.util.List;
5+
6+
public class LambdaForeach2 {
7+
public static void main(String[] args) {
8+
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7);
9+
list.forEach(x->{
10+
if(x>2){
11+
System.out.println(x);
12+
}
13+
});
14+
15+
}
16+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.lambda;
2+
3+
public class LambdaRunnable {
4+
public static void main(String[] args) {
5+
Runnable race2 = () -> System.out.println("Hello world !");
6+
race2.run();
7+
8+
// 传统的代码表现方式如下:
9+
// Runnable race1 = new Runnable() {
10+
// @Override
11+
// public void run() {
12+
// System.out.println("Hello world !");
13+
// }
14+
// };
15+
// race1.run
16+
17+
18+
}
19+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.lambda;
2+
3+
import java.util.Arrays;
4+
import java.util.Collections;
5+
import java.util.Comparator;
6+
7+
public class LambdaSort {
8+
public static void main(String[] args) {
9+
String[] players = {"Rafael Nadal", "Novak Djokovic",
10+
"Richard Gasquet", "John Isner"};
11+
12+
// 1.2 使用 lambda expression 排序 players
13+
Comparator<String> sortByName = (String s1, String s2) -> (s1.compareTo(s2));
14+
Arrays.sort(players, sortByName);
15+
16+
17+
// 1.3 也可以采用如下形式:
18+
// Arrays.sort(players, (String s1, String s2) -> (s1.compareTo(s2)));
19+
20+
21+
}
22+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.lambda;
2+
3+
public class LambdaThread {
4+
public static void main(String[] args) {
5+
new Thread(
6+
() -> System.out.println("New thread")
7+
).start();
8+
System.out.println("Main thread");
9+
}
10+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.lambda;
2+
3+
import java.util.Arrays;
4+
import java.util.List;
5+
6+
public class StreamMap {
7+
public static void main(String[] args) {
8+
List<Integer> list = Arrays.asList(1,2,3,4,5,6,7);
9+
list.stream().map( (x) -> x*x ).forEach(System.out::println);
10+
}
11+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.lambda;
2+
3+
import java.util.Arrays;
4+
import java.util.List;
5+
6+
public class StreamReduce {
7+
public static void main(String[] args) {
8+
List<Integer> list = Arrays.asList(1,2,3,4,5,6,7);
9+
int sum = list.stream().map( x -> x*x ).reduce( (x,y) -> x + y).get();
10+
System.out.println(sum);
11+
12+
// 传统的代码表现方式如下:
13+
// List<Integer> list = Arrays.asList(1,2,3,4,5,6,7);
14+
// int sum = 0;
15+
// for(Integer n : list) {
16+
// int x = n * n;
17+
// sum = sum + x;
18+
// }
19+
// System.out.println(sum);
20+
21+
}
22+
}

0 commit comments

Comments
 (0)