Skip to content

Commit 3bb34d8

Browse files
committed
Add examples with replacing lambdas to method references
1 parent 1f54f20 commit 3bb34d8

1 file changed

Lines changed: 37 additions & 0 deletions

File tree

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package io.github.aplotnikov.java_8_misuses.lambda;
2+
3+
import java.util.Optional;
4+
5+
import static io.github.aplotnikov.java_8_misuses.utils.Annotations.Good;
6+
import static io.github.aplotnikov.java_8_misuses.utils.Annotations.Ugly;
7+
8+
class LambdasAreNotAlwaysTheBestOption {
9+
10+
@Ugly
11+
class UnneededLambdasUsage {
12+
void processAndPrint(String name) {
13+
Optional.ofNullable(name)
14+
.map(s -> s.toUpperCase())
15+
.map(s -> doProcess(s))
16+
.ifPresent(s -> System.out.print(s));
17+
}
18+
19+
private String doProcess(String name) {
20+
return "MR. " + name;
21+
}
22+
}
23+
24+
@Good
25+
class MethodReferenceUsage {
26+
void processAndPrint(String name) {
27+
Optional.ofNullable(name)
28+
.map(String::toUpperCase)
29+
.map(this::doProcess)
30+
.ifPresent(System.out::print);
31+
}
32+
33+
private String doProcess(String name) {
34+
return "MR. " + name;
35+
}
36+
}
37+
}

0 commit comments

Comments
 (0)