Метод flatMapping ((блюдо) -> {}, toSet ()) не определено для типа группировки - PullRequest
0 голосов
/ 03 февраля 2019

Я использую Java 8 и получаю ошибку ниже для кода в строке-30

Метод flatMapping ((dish) -> {}, toSet ()) не определен для типа группировки

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

1 Ответ

0 голосов
/ 03 февраля 2019

Вероятно, из-за неверного типа возврата, который вы ожидаете, реализация метода должна выглядеть примерно так:

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

Примечание : Iввел переменную в качестве параметра только для ответа.

Важно : API flatMapping в Collectors быловведен с Java-9 .

...