The simplest way to comma-delimit a list?

Java 8 and later

Using StringJoiner class, and forEach method :

StringJoiner joiner = new StringJoiner(",");
list.forEach(item -> joiner.add(item.toString());
return joiner.toString();

Using Stream, and Collectors:

return list.stream().
       map(Object::toString).
       collect(Collectors.joining(",")).toString();

Java 7 and earlier

See also #285523

String delim = "";
for (Item i : list) {
    sb.append(delim).append(i);
    delim = ",";
}

Leave a Comment