我正在使用 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引入的。
添加回答
举报
0/150
提交
取消
