Skip to content
Advertisement

Convert a for loop to concat String into a lambda expression

I have the following for loop which iterates through a list of strings and stores the first character of each word in a StringBuilder. I would like to know how can I transform this to a lambda expression

JavaScript

Advertisement

Answer

Assuming you call toString() on the StringBuilder afterwards, I think you’re just looking for Collectors.joining(), after mapping each string to a single-character substring:

JavaScript

Sample code:

JavaScript

Note the use of substring instead of charAt, so we still have a stream of strings to work with.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement