gpt4 book ai didi

java - Grouping 类型未定义方法 flatMapping(( dish) -> {}, toSet())

转载 作者:行者123 更新时间:2023-11-30 10:06:47 24 4
gpt4 key购买 nike

我使用的是 Java 8,第 30 行的代码出现以下错误

The method flatMapping(( dish) -> {}, toSet()) is undefined for the type Grouping

public class Grouping {
enum CaloricLevel { DIET, NORMAL, FAT };

public static void main(String[] args) {
System.out.println("Dishes grouped by type: " + groupDishesByType());
System.out.println("Dish names grouped by type: " + groupDishNamesByType());
System.out.println("Dish tags grouped by type: " + groupDishTagsByType());
}


private static Map<Type, List<Dish>> groupDishesByType() {
return Dish.menu.stream().collect(groupingBy(Dish::getType));
}

private static Map<Type, List<String>> groupDishNamesByType() {
return Dish.menu.stream().collect(groupingBy(Dish::getType, mapping(Dish::getName, toList())));
}


private static String groupDishTagsByType() {
/*line:30*/ return menu.stream().collect(groupingBy(Dish::getType, flatMapping(dish -> dishTags.get( dish.getName() ).stream(), toSet())));
}
}

enter image description here

最佳答案

这可能是因为您期望的返回类型不正确,方法实现应该类似于:

private static Map<Dish.Type, Set<String>> groupDishTagsByType(Map<String, List<String>> dishTags) {
return Dish.menu.stream()
.collect(Collectors.groupingBy(Dish::getType,
Collectors.flatMapping(dish -> dishTags.get(dish.getName()).stream(),
Collectors.toSet())));
}

注意:我将变量作为参数引入只是为了回答。

重要:flatMapping API in Collectors是随 Java-9 引入的。

关于java - Grouping 类型未定义方法 flatMapping((<no type> dish) -> {}, toSet()),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54505423/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com