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.

Noncompliant Code Example

  List<Integer> list = new ArrayList<Integer>();
  list.add(0);
  list.add(1);
  list.add(2);

  list.forEach(n -> { System.out.println(n); });

Compliant Solution

  List<Integer> list = new ArrayList<Integer>();
  list.add(0);
  list.add(1);
  list.add(2);

  list.forEach(System.out::println);