'Why does mapMulti need type information in comparison to flatMap
I want to use mapMulti instead of flatMap and refactored the following code:
// using flatMap (version 1) => returns Set<Item>
var items = users.stream()
.flatMap(u -> u.getItems().stream())
.collect(Collectors.toSet());
into this (version 2):
// using mapMulti (version 2) => returns Set<Item>
var items = users.stream()
.<Item>mapMulti((u, consumer) -> u.getItems().forEach(consumer))
.collect(Collectors.toSet());
Both return the same elements. However, I am in doubt if I should really replace all my flatMap with that more verbose code of mapMulti. Why do I need to add the type information before mapMuli (.<Item>mapMulti). If I don't inlcude the type information, it will return a Set<Object>. (How) can I simplify mapMulti?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
