Java 8: Array Filtering (NxM) to create a map <String, HashSet <String>>

I would like to map an NxN array in Map in Java 8.

The idea is that each element [i] [0] is a key, and each [i] [j] with j> 0 is a list of values ​​for each key on the map.

Thanks for any help. :)

This is my class:

public class GroupingDishes {

    public static void main(String[] args) {

        String[][] dishes = {
                {"Salad", "Tomato", "Cucumber", "Salad", "Sauce"},
                {"Pizza", "Tomato", "Sausage", "Sauce", "Dough"},
                {"Quesadilla", "Chicken", "Cheese", "Sauce"},
                {"Sandwich", "Salad", "Bread", "Tomato", "Cheese"}
        };

        Map<String, HashSet<String>> groupDishes = groupingDishes(dishes);
    }

    public static Map<String, HashSet<String>> groupingDishes(String[][] dishes) {

        Map<String, HashSet<String>> mapFood = new HashMap<>();

        for (int i = 0; i < dishes.length; i++) {

            String food = dishes[i][0];

            for (int j = 0; j < dishes[i].length; j++) {

                if (mapFood.containsKey(food)) {

                    HashSet<String> existingIngredients = mapFood.get(dishes[i][0]);
                    existingIngredients.add(dishes[i][j]);
                    mapFood.put(food, existingIngredients);

                } else {

                    HashSet<String> newIngredient = new HashSet<>();
                    mapFood.put(food, newIngredient);

                }
            }
        }
        return mapFood;
    }
}
+4
source share
2 answers

You can convert String[][]to a stream String[], then collect the card using the first element String[]as a key, and the rest as values ​​of the set.

public static Map<String, HashSet<String>> groupingDishes2(String[][] dishes) {
    return Arrays.stream(dishes)
        .collect(Collectors.toMap(
            arr -> arr[0],
            arr -> Arrays.stream(arr).skip(1).collect(Collectors.toCollection(HashSet::new))));
}

Btw, I doubt you really need Map<String, HashSet<String>>. It would be better to change the types to Map<String, Set<String>>, and then the implementation could be written easier.

public static Map<String, Set<String>> groupingDishes(String[][] dishes) {
    return Arrays.stream(dishes)
        .collect(Collectors.toMap(
            arr -> arr[0],
            arr -> Arrays.stream(arr).skip(1).collect(Collectors.toSet())));
}

, @Holger, , " skip limit , Collectors ":

public static Map<String, Set<String>> groupingDishes(String[][] dishes) {
    return Arrays.stream(dishes)
        .collect(Collectors.toMap(
            arr -> arr[0],
            arr -> new HashSet<>(Arrays.asList(arr).subList(1, arr.length))));
}
+5

:

Map<String, HashSet<String>> result = Arrays.stream(dishes)
            .map(x -> new AbstractMap.SimpleEntry<>(
                    x[0],
                    Arrays.stream(x).skip(1).collect(Collectors.toCollection(HashSet::new))))
            .collect(Collectors.toMap(Entry::getKey, Entry::getValue));
+2

Source: https://habr.com/ru/post/1689710/


All Articles