Convert a for loop to concat String into a lambda expression

后端 未结 5 1691
予麋鹿
予麋鹿 2021-02-03 22:37

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

5条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-02-03 23:19

    Tons of ways to do this - the most simple option: stick to adding to a StringBuilder and do this:

    StringBuilder chars = new StringBuilder();
    
    list.forEach(l -> chars.append(l.charAt(0)));
    

提交回复
热议问题