When to use map and flatMap?
map(): It is used where we have to map the elements of a particular collection to a specific function, and then we need to return the stream that contains the updated results.
Example: Multiply all the elements of a list by 3 and return the updated list.
flatMap(): It is used where we have to transform or flatten the string, as we can't flatten our string using map().
Example: Get the first Character of all the String present in a List of Strings and return the result in form of a stream.
1January 17, 2026 3.1K 3