Skip to content
Advertisement

Counting items from Map having Set as value

I have a list of items as below

JavaScript

Now I am converting this list into

JavaScript

here complexList gives output as

JavaScript

Now I need to count number of values for each “capacity” giving output as

JavaScript

I tried

JavaScript

and it outputs

JavaScript

I must be mistaking in understanding streams or not be using right methods. Can anyone suggest an approach or a solution? Reference link to streams will also be helpful.

Advertisement

Answer

if all you want to do is print each key of the map along with the size of the corresponding value, then there is no need to stream again as it causes unnecessary overhead. simply iterate overly the complexList and print it like so:

JavaScript

or if you really want a map then one could also do:

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement