How to convert a List<String> list to csv string

Apache Commons Lang contains a StringUtils.join() method for precisely this purpose. Note that different flavours exist.

And as of March 2014, Java 8 now has a StringJoiner


If you are using Java 8

List<String> objects= Arrays.asList("Test1","Test2","Test3");
String objectsCommaSeparated = String.join(",", objects);
System.out.println(objectsCommaSeparated);

With Streams

String objectsCommaSeparated = objects.stream().collect(Collectors.joining(","));
System.out.println(objectsCommaSeparated);

There is a Guava class called Joiner that can easily create these kind of Strings.

Do Joiner.on(",").join(yourStrings)

Tags:

Java

Csv

String