This example will show how to print a list of objects separating each element with a comma using java, java 8, guava, apache commons and spring framework. In the set up method we will define a list using guava's List collection utility named programmingLanguages and the expected output in string variable named languagesSeperatedByComma.
Setup
Straight up Java
This snippet will show how to print out all the elements of a list separating them with a comma using straight up java. The join method will accept a separator and vararg of strings to join together. The test method will call join passing in the ArrayList and a comma as a separator.
Java 8
Using java 8 string joiner we will join a stream using Collectors.joining which will concatenates the input elements separated by specified delimiter, in this case a comma.
Google Guava
This snippet will show how to join a collection of strings separating each element by a comma using Guava's Joiner object which joins pieces of text with a separator.
Apache Commons
This snippet will demonstrate how to convert a programmingLanguages arraylist into a comma separated value string using apache commons StringUtils.join. It will join the elements of the provided Iterable into a single String.
Spring Framework
Springs implementation StringUtils.collectionToDelimitedString provides the same behavior above. This connivence method will return a arraylist as a string separated by a comma.