There are a couple of issues with your current approach.
- There is no toMap method with a signature
toMap()to display a compilation error. - flatMap expects a function with a type
Tand returns Stream<R>, while you are trying to pass the map as a return value, to also throw a compilation error.
Instead, it seems that you want something like this:
Map<String, List<String>> resultSet = map.entrySet()
.stream()
.flatMap(entry -> Arrays.stream(entry.getValue())
.filter(Channel::isValid)
.map(e -> new AbstractMap.SimpleEntry<>(entry.getKey(), e)))
.collect(Collectors.groupingBy(AbstractMap.SimpleEntry::getKey,
Collectors.mapping(AbstractMap.SimpleEntry::getValue,
Collectors.toList())));
or a much simpler solution:
map.entrySet()
.stream()
.collect(Collectors.toMap(Map.Entry::getKey,
a -> Arrays.stream(a.getValue())
.filter(Channel::isValid)
.collect(Collectors.toList())));
source
share