Stream groupingBy: reducing to first element of list
Actually, you need to use Collectors.toMap
here instead of Collectors.groupingBy
:
Map<String, Valuta> map =
getValute().stream()
.collect(Collectors.toMap(Valuta::getCodice, Function.identity()));
groupingBy
is used to group elements of a Stream based on a grouping function. 2 Stream elements that will have the same result with the grouping function will be collected into a List
by default.
toMap
will collect the elements into a Map
where the key is the result of applying a given key mapper and the value is the result of applying a value mapper. Note that toMap
, by default, will throw an exception if a duplicate is encountered.
The toMap
version which opts to choose the 1st value on collisions instead of throwing an exception is:
Collectors.toMap(keyMapper, valueMapper, mergeFunction)
ex:
Map<String, Valuta> map = list
.stream()
.collect(Collectors.toMap(Valuta::getCodice, v -> v, (v1, v2) -> v1));
You could use Collectors.toMap(keyMappingFunction, valueMappingFunction)
Map<String, Valuta> map = list
.stream()
.collect(Collectors.toMap(Valuta::getCodice, v -> v));
You can replace v->v
with Function.identity()
if you find it more readable. Note that toMap
, by default, will throw an exception if a duplicate is encountered.
It's a bit late in the game, but try this:
Map<String, Valuta> map =
getValute().stream()
.collect(Collectors.groupingBy(Valuta::getCodice,
Collectors.collectingAndThen(
Collectors.toList(),
values -> values.get(0))));