35 lines
940 B
Plaintext
35 lines
940 B
Plaintext
Before Java 8, the only way to partially support closures in Java was by using anonymous inner classes. But the syntax of anonymous classes may seem unwieldy and unclear.
|
|
|
|
|
|
With Java 8, most uses of anonymous inner classes should be replaced by lambdas to highly increase the readability of the source code.
|
|
|
|
|
|
*Note* that this rule is automatically disabled when the project's ``++sonar.java.source++`` is lower than ``++8++``.
|
|
|
|
|
|
== Noncompliant Code Example
|
|
|
|
----
|
|
myCollection.stream().map(new Mapper<String,String>() {
|
|
public String map(String input) {
|
|
return new StringBuilder(input).reverse().toString();
|
|
}
|
|
});
|
|
|
|
Predicate<String> isEmpty = new Predicate<String> {
|
|
boolean test(String myString) {
|
|
return myString.isEmpty();
|
|
}
|
|
}
|
|
----
|
|
|
|
|
|
== Compliant Solution
|
|
|
|
----
|
|
myCollection.stream().map(input -> new StringBuilder(input).reverse().toString());
|
|
|
|
Predicate<String> isEmpty = myString -> myString.isEmpty();
|
|
----
|
|
|