What is the purpose of the flatMap() method in the Stream API?

  • To collect the elements of the stream into a list
  • To filter elements based on a given predicate
  • To flatten a stream of streams into a single stream
  • To perform an operation on each element in the stream
The flatMap() method in the Stream API is used to flatten a stream of streams into a single stream. It is particularly useful when you have a stream of elements, and each element is itself a stream (e.g., a stream of lists). flatMap() will merge all these sub-streams into one, providing a single stream of elements. This is different from map(), which produces a stream of streams.
Add your answer
Loading...

Leave a comment

Your email address will not be published. Required fields are marked *