Using StringJoiner
class :
StringJoiner joiner = new StringJoiner(",");
for (Item item : list) {
joiner.add(item.toString());
}
return joiner.toString();
Using Stream
, and Collectors
:
return list.stream().
map(Object::toString).
collect(Collectors.joining(",")).toString();
See also #285523
String delim = "";
for (Item i : list) {
sb.append(delim).append(i);
delim = ",";
}