Take a look at this piece of code.
// group by price, uses \'mapping\' to convert List- to Set
Map&
Is groupingBy and Mapping interchangeable?
No, they are completely different. groupingBy
lets you create a Map
where the key is the first argument passed to groupingBy
and the value is a List
of the element type of the Stream
.
Collectors.groupingBy(Item::getPrice)
would generate a Map
(assuming Item::getPrice
returns a BigDecimal
. Passing the mapping
Collector
as an argument to Collectors.groupingBy()
allows you to change the value of the output map (in your example, you change it to Set
).
For the third parameter in collect(), would I get the same output type Map if I used Collectors.toList() instead of Collectors.toSet()?
No, you would get a Map
.