java - 方法 flatMapping((disc) -> {}, toSet()) 未为 Grouping 类型定义
问题描述
我正在使用 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())));
}
}
解决方案
这可能是因为您期望的返回类型不正确,方法实现应该看起来像:
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引入的。
推荐阅读
- python - Gunicorn 和自定义记录器
- javascript - vue-splide 仅适用于 Nuxt 开发环境
- python - 烧瓶 - jinja2.exceptions.TemplateNotFound
- java - 为什么没有 LinkedList.join() 方法可以在 O(1) 时间内合并两个未排序的列表?
- python - 如何在天气命令中添加差异变量
- api - 如何在 Flutter Web 中进行 get 调用并将响应导出到具有特定标头和过滤数据的 csv 文件中
- hybris - Hybris - 结帐 - 对象不再有效错误
- html - 在 Wordpress 中集成代码片段(Html 和 Css)
- reactjs - 停留在显示更新的数组数据
- r - 如何在散点图矩阵中留下具有非显着值的空白单元格?