Search code examples
javafunctional-interfacefunctional-java

Seeking in-depth understanding of Function<T,R>


Shown below is some sample code that uses Java Streams. My question specifically pertains to Interface Function<T,R> which accepts an input of type T and returns something of type R.

import static java.util.Arrays.asList;
import static java.util.stream.Collectors.groupingBy;

import java.util.List;
import java.util.Map;

public class Dish {

  private final String name;
  private final boolean vegetarian;
  private final String calories;

  public Dish(String name, boolean vegetarian, String calories) {
    this.name = name;
    this.vegetarian = vegetarian;
    this.calories = calories;
  }

  public String getName() {
    return name;
  }

  public boolean isVegetarian() {
    return vegetarian;
  }

  public String getCalories() {
    return calories;
  }

  @Override
  public String toString() {
    return name;
  }

  public static final List<Dish> menu = asList(
      new Dish("pork", false, "GE 600"),
      new Dish("beef", false, "GE 600"),
      new Dish("chicken", false, "300-600"),
      new Dish("french fries", true, "300-600"),
      new Dish("rice", true, "LE 300"),
      new Dish("season fruit", true, "LE 300"),
      new Dish("pizza", true, "300-600"),
      new Dish("prawns", false, "300-600"),
      new Dish("salmon", false, "300-600")
  );

  public static void main(String[] args) {
        Map<String, List<Dish>> dishByCalories = menu.stream()
                .collect(groupingBy(Dish::getCalories));
        System.out.println(dishByCalories);
  }
}

Obviously groupingBy(Dish::getCalories) is meeting the expected method signature requirements of collect (i.e. Collector<? super T,A,R> collector)

Now coming to groupingsBy, its signature requirements are the following:
static <T,K> Collector<T,?,Map<K,List<T>>> groupingBy(Function<? super T,? extends K> classifier)

The method reference that we are passing to groupingsBy is Dish::getCalories

Obviously Dish::getCalories is meeting the signature requirements of Function<? super T,? extends K> (i.e. it should accept an input of some superclass of T and return a result of some subclass of K).

However, the getCalories method does not accept any arguments and it returns a String.

Kindly help clear my confusion.


Solution

  • getCalories is an instance method, so it accepts the implicit this argument.

    Dish::getCalories is equivalent to lambda (Dish dish) -> dish.getCalories(), which makes it an implementation of Function<Dish, String>.