Method/constructor references are more compact and readable than using lambdas, and are therefore preferred. Similarly, null
checks
can be replaced with references to the Objects::isNull
and Objects::nonNull
methods.
Note that this rule is automatically disabled when the project's sonar.java.source
is lower than 8
.
class A { void process(List<A> list) { list.stream() .map(a -> a.<String>getObject()) .forEach(a -> { System.out.println(a); }); } <T> T getObject() { return null; } }
class A { void process(List<A> list) { list.stream() .map(A::<String>getObject) .forEach(System.out::println); } <T> T getObject() { return null; } }