深入了解Function

问题描述 投票:0回答:2

下面显示的是一些使用Java Streams的示例代码。我的问题特别涉及Interface Function<T,R>,它接受类型为T的输入并返回类型为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);
  }
}

显然groupingBy(Dish::getCalories)满足collect(即Collector<? super T,A,R> collector)的预期方法签名要求

现在进入groupingsBy,其签名要求如下:static <T,K> Collector<T,?,Map<K,List<T>>> groupingBy(Function<? super T,? extends K> classifier)

我们传递给groupingsBy的方法参考是Dish::getCalories

[显然Dish::getCalories满足Function<? super T,? extends K>的签名要求(即,它应接受T的某些超类的输入并返回K的某个子类的结果)。

但是,getCalories方法不接受任何参数,它返回一个字符串。

请帮助消除我的困惑。

java functional-interface functional-java
2个回答
2
投票

getCaloriesinstance


0
投票

Language specification开始,正如@Andreas指出,每个实例方法都隐式地将this作为第一个参数接受

© www.soinside.com 2019 - 2024. All rights reserved.