Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 33975aa

Browse filesBrowse files
committed
Lambda stream流
1 parent 59ee2dc commit 33975aa
Copy full SHA for 33975aa

File tree

Expand file treeCollapse file tree

2 files changed

+77
-0
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

2 files changed

+77
-0
lines changed
Open diff view settings
Collapse file
+37Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.learnjava.lambda;
2+
3+
/**
4+
* @author bruis
5+
* lambda表达式
6+
*/
7+
public class LambdaDemo01 {
8+
9+
/**
10+
* 打印内部类
11+
*/
12+
interface Printer {
13+
void print(String content);
14+
// void print(String content, String operator);
15+
}
16+
17+
public static void printSomething(String content, Printer printer) {
18+
printer.print(content);
19+
}
20+
21+
public static void main(String[] args) {
22+
// Printer printer = (String content) -> {
23+
// System.out.println(content);
24+
// };
25+
26+
// 去掉参数类型,只有一个参数时可以去掉括号
27+
// Printer printer = (content) -> {
28+
// System.out.println(content);
29+
// };
30+
31+
// 只有一个参数提
32+
// Printer printer = val -> System.out.println(val);
33+
34+
Printer printer = System.out::println;
35+
printSomething("hello lambda", printer);
36+
}
37+
}
Collapse file
+40Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.learnjava.lambda;
2+
3+
import jdk.nashorn.internal.objects.annotations.Constructor;
4+
5+
import java.util.Arrays;
6+
import java.util.List;
7+
import java.util.stream.Collectors;
8+
9+
/**
10+
* @author bruis
11+
* Lambda的List demo
12+
*/
13+
public class LambdaDemoForList {
14+
15+
public static void main(String[] args) {
16+
test01();
17+
}
18+
19+
public static void test01() {
20+
List<String> nameStrList = Arrays.asList("abc", "efg", "hig", "hii", "klm");
21+
22+
List<String> result = nameStrList
23+
.stream()
24+
.filter(s -> s.startsWith("h"))
25+
.map(String::toUpperCase)
26+
.map(MyStringUtils::myToUpperCase)
27+
.collect(Collectors.toList());
28+
29+
for (String name : result) {
30+
System.out.println(name);
31+
}
32+
}
33+
34+
private static class MyStringUtils {
35+
36+
public static String myToUpperCase(String str) {
37+
return str.toUpperCase();
38+
}
39+
}
40+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.