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
Without creating many intermediate String objects you can do it like this:
StringBuilder sb = list.stream()
.mapToInt(l -> l.codePointAt(0))
.collect(StringBuilder::new,
StringBuilder::appendCodePoint,
StringBuilder::append);
Note that using codePointAt is much better than charAt as if your string starts with surrogate pair, using charAt you may have an unpredictable result.