方法 flatMapping((<no type> disc) -> {}, toSet())

我正在使用 Java 8 并且在第 30 行的代码出现以下错误


方法 flatMapping(( disc) -> {}, toSet()) 对于 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())));

    }

}


慕容森
浏览 111回答 1
1回答

慕哥6287543

这可能是因为您期望的返回类型不正确,方法实现应该看起来像:private static Map<Dish.Type, Set<String>> groupDishTagsByType(Map<String, List<String>> dishTags) {&nbsp; &nbsp; return Dish.menu.stream()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .collect(Collectors.groupingBy(Dish::getType,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Collectors.flatMapping(dish -> dishTags.get(dish.getName()).stream(),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Collectors.toSet())));}注意:我将变量作为参数引入只是为了回答。重要提示:flatMapping APICollectors是随Java-9引入的。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java