Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java 8: Filter an Array(NxM) to create a Map<String, HashSet<String>>

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

The idea is that every [i][0] element is a key and every [i][j] with j>0 is a list of values for every key in 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;
    }
}
like image 208
Francisco Garido Mosqueira Avatar asked Jan 30 '23 04:01

Francisco Garido Mosqueira


1 Answers

You could convert String[][] to a stream of String[], then collect to a map, using the first item of the String[] as the key, and the rest as the 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 a Map<String, HashSet<String>>. It would be better to change the types to Map<String, Set<String>>, and then the implementation can be written simpler too.

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

Or even better, as @Holger suggested, an even better alternative, because "streams with skip and limit do not perform very well, also Collectors do not get any hint for the result's initial capacity":

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))));
}
like image 172
janos Avatar answered Feb 02 '23 10:02

janos