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

PAA :

I am using Java 8 and getting below error for the code on line-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())));
    }
}

enter image description here

Naman :

That's probably because of an incorrect return type that you're expecting there, the method implementation should look somewhat like:

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())));
}

Note: I've brought in the variable as a parameter just for the answer.

Important: The flatMapping API in Collectors was introduced with Java-9.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=160528&siteId=1