What is the difference between `Stream.map()` and `Stream.flatMap()` in Java?

Java Associate Hard

Java Associate — Hard

What is the difference between `Stream.map()` and `Stream.flatMap()` in Java?

Key points

  • map() transforms elements to new values
  • flatMap() flattens nested streams
  • flatMap() is useful for dealing with nested collections

Ready to go further?

Related questions