Imagine a multimap tracking persons, each of whom has a list of assigned tasks.
Map< Person , List< Task > > personToTasks =
Map.of(
new Person( "Alice" ) , List.of( new Task( "a1" ), new Task( "a2") ) ,
new Person( "Bob" ) , List.of( new Task( "b1" ) ) ,
new Person( "Carol" ) , List.of( new Task( "c1" ), new Task( "c2"), new Task( "c3") )
)
;
How can I use streams to get a new map, mapping each Person
to an Integer
with the count of items found in their list of assigned tasks?
How to get a result equivalent to this hard-coded map:
Map< Person , Integer > personToTaskCount =
Map.of(
new Person( "Alice" ) , 2 ,
new Person( "Bob" ) , 1 ,
new Person( "Carol" ) , 3
)
;
I have been trying permutations of:
Map < Person, Integer > personToTaskCount =
personToTasks.keySet().stream().collect
(
Collectors.mapping
(
Map.Entry :: getKey ,
???
)
)
;