Stream a set of Strings into Map of <String, ArrayList()>

Viewed 137

I have a set of Strings (it's static) and I would like to create a Map<String, List<String>> from them, where the list is initialized to a new ArrayList. Something like the following, but this code is not correct.

static Set<String> MY_TYPES = Set.of("type1", "type2", "type3");

Map<String, List<String> myMap = MY_TYPES.stream().map(t->() t, new ArrayList<>()).collect(Collectors.toMap());

I am trying to learn how to use streams better in my code. I know I can do this iterating over the Set.

Edit

@shmosel, you are correct. I want the equivalent of

Map.of("type1", new ArrayList<>(), "type2", new ArrayList<>(), "type3", new ArrayList());
1 Answers

Sounds like you're looking for this:

MY_TYPES.stream()
        .collect(Collectors.toMap(s -> s, s -> new ArrayList<>()))
Related