Cosaic :
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.
Patrick Parker :
using Collectors.toMap:
Map<String, Transaction> transactions = transactions.stream()
.collect(Collectors.toMap(Transaction::getID, Function.identity()));