为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

慕容森 2022-06-04 15:02:51
我正在使用 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())));    }}
查看完整描述

1 回答

?
慕哥6287543

TA贡献1831条经验 获得超10个赞

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


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 APICollectors是随Java-9引入的。


查看完整回答
反对 回复 2022-06-04
  • 1 回答
  • 0 关注
  • 110 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号