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.

Noncompliant Code Example

myCollection.map(new Mapper() {
  public String map(String input) {
    return new StringBuilder(input).reverse().toString();
  }
});

Compliant Solution

myCollection.map(element -> new StringBuilder(element).reverse().toString());