Using flatMap() method of Stream object in Java

When working with Stream in Java, sometime we can have a Stream with datatype of a List, Set or Array of object. For example:

Here, we have List of List of String object and when convert into Stream object, we have Stream of List of String object.

When that, if we want to manipulate on this Stream object like filter, then we cannot do that. For example:

Using flatMap() method of Stream object in Java

To resolve this problem, we can use flatMap() method before the manipulation you need. With flatMap() method, we can flatten our Stream of List, Set or Array object into a simple one like Stream of object. For example:

Now, we can use flatMap() method to resolve our problem.

Using flatMap() method of Stream object in Java

4.5/5 - (2 votes)

Add Comment