San*_*eep 0 java functional-java functional-interface
下面显示的是一些使用Java Streams的示例代码。我的问题专门涉及Interface Function<T,R>接受type的输入T并返回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);
}
}
Run Code Online (Sandbox Code Playgroud)
显然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方法不接受任何参数,它返回一个String。
请帮助消除我的困惑。
getCalories是一个实例方法,因此它接受隐式this参数。
Dish::getCalories等价于lambda (Dish dish) -> dish.getCalories(),这使其成为的实现Function<Dish, String>。
| 归档时间: |
|
| 查看次数: |
53 次 |
| 最近记录: |