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

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

我正在使用 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())));
    }
}

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

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 CollectorsJava-9[= 一起引入25=].