I know the groupingBy
return a Map<K,List<V>>
. But If I know that each key has a unique value, how do I get a Map whose value is V
instead of List<V>
?
For example:
Map<String, Transaction> transactions =
transactions.stream().collect(groupingBy(Transaction::getID));
where ID is unique.
using Collectors.toMap:
Map<String, Transaction> transactions = transactions.stream()
.collect(Collectors.toMap(Transaction::getID, Function.identity()));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With