Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why streams cannot be created for map in Java8?

Why we can't create the streams for map?

like image 312
Shanki Bansal Avatar asked Sep 02 '26 10:09

Shanki Bansal


1 Answers

Streams cannot directly be created from maps because a map is not a collection. For further explanation on why a map is not a collection you can view this answer https://stackoverflow.com/a/2651833/2796463.

Maps in Java can be iterated in three ways:

  1. A set of keys
  2. A collection of values
  3. A a set of key value pairs

You need to specify which order you wish to iterate through the map before creating a stream

  1. map.keySet().stream()
  2. map.values().stream()
  3. map.entrySet().stream()
like image 138
DragonAssassin Avatar answered Sep 05 '26 01:09

DragonAssassin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!