Skip to content

Lambda Selection

Lambdas are based on interfaces, and the compiler relies on context to determine what a lambda means.

  • Context is either the variable type we're assigning a lambda to, or method parameter type when we pass a lambda in a method call.

Consider the PlanetTester interface.

public interface PlanetTester {
  public boolean test(Planet p);
}

This method takes a List of Planets and a PlanetTester interface implementation.

public static List<Planet> filterPlanets(List<Planet> list, PlanetTester ps) {
  List<Planet> tempList = new ArrayList<>();
  for (Planet planet : list) {
    // Use the PlanetTester implementation
    if (ps.test(planet)) {    
      tempList.add(planet);
    }
  }
  return tempList;
}

When we call the method and pass a lambda, Java knows we are trying to implement PlanetTester's one method, boolean test(Planet).

List<Planet> bigPlanets = filterPlanets(planets, p -> p.getDiameter() > 40_000);

The compiler can map the lambda expression to boolean test(Planet) because PlanetTester is a functional interface.

Functional Interface

A functional interface contains only one abstract method.

  • The compiler knows which method a lambda is trying to implement because there is only one to implement.

The compiler looks at the interface and its single abstract method to translate a lambda to a method.

Lambda to method to method body

@FunctionalInterface

We can add the annotation @FunctionalInterface to an interface to ask the compiler to generate an error if there is not exactly one abstract method.

@FunctionalInterface
public interface PlanetTester {
  public boolean test(Planet p);
}

Drill

Lambdas/com.example.lambdas.drills.PlanetUtilities

Add the method filterPlanets to PlanetUtilities.

public static List<Planet> filterPlanets(List<Planet> list, PlanetTester ps) {
  List<Planet> tempList = new ArrayList<>();
  for (Planet planet : list) {
    // Use the PlanetTester implementation
    if (ps.test(planet)) {    
      tempList.add(planet);
    }
  }
  return tempList;
}

com.example.lambdas.drills.FilterPlanets

  • Call PlanetUtilities.filterPlanets to filter the list of Planets for those whose diameter is less than 40_000.
  • Print the list of small planets.

Prev -- Up -- Next