4

Why we can't create the streams for map?

Sartorius
  • 499
  • 5
  • 12
Shanki Bansal
  • 1,681
  • 2
  • 22
  • 32

1 Answers1

11

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()
Community
  • 1
  • 1
DragonAssassin
  • 929
  • 2
  • 10
  • 14
  • 1
    Good answer, but it would be even stronger if you in a concrete way answered the original question (_Why we can't create the streams for map?_). – Magnilex Oct 10 '16 at 08:09