Skip to content
Advertisement

How to collect data to List from Map using java Stream API?

I have map Map<Nominal, Integer> with objects and their counts:

JavaScript

And I need to get such a List<Nominal> from it:

JavaScript

How can I do this using the Stream API?

Advertisement

Answer

We can use Collections::nCopies to achieve the desired result:

JavaScript

Ideone demo


Remark

In the demo, I changed the key-type of the Map from Nominal to Object since the definition of Nominal was not provided. Changing the key-type, however, does not influence the solution.

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