Take a look at this piece of code.
// group by price, uses 'mapping' to convert List<Item> to Set<String>
Map<BigDecimal, Set<String>> result =
items.stream().collect(
Collectors.groupingBy(Item::getPrice,
Collectors.mapping(Item::getName, Collectors.toSet())
)
);
Is groupingBy and Mapping interchangeable? What is their differences?
For the third parameter in collect(), would I get the same output type Map if I used Collectors.toList() instead of Collectors.toSet()? I heard that toList() is a more popular option.
No, the two are completely different.
Collectors.groupingBy
takes a function which creates keys and returns a collector which returns a map from keys to collections of objects in the stream which have that same key.
Collectors.mapping
, on the other hand, takes a function and another collector, and creates a new collector which first applies the function and then collects the mapped elements using the given collectors. Thus, the following are equivalent:
items.stream().map(f).collect(c);
items.stream().collect(Collectors.mapping(f, c));
Collectors.mapping
is most useful in situations where you do not have a stream, but you need to pass a collector directly. An example of such a situation is when using Collectors.groupingBy
.
items.stream().collect(Collectors.groupingBy(Item::getPrice, Collectors.toSet()))
yields a Map<BigDecimal,Set<Item>>
(assuming getPrice()
returns a BigDecimal
). However,
items.stream().collect(Collectors.groupingBy(Item::getPrice,
Collectors.mapping(Item::getName, Collectors.toSet())))
returns a Map<BigDecimal,Set<String>>
. Before collecting the items, it first applies Item.getName
to them.