How To Join List String With Commas In Java
Displaying a list of values with separators is a quite common task. Since Java 8 this is quite easy to do with Java board tools. Before that, it was necessary to write your own method or use a library like Google Guava or Apache Commons.
How To Join List String With Commas Using String.join() In Java 8
Since Java 8, the String class has a static method called join(). This method takes the separator and the list of strings, as shown in the example bellow:
List names = Arrays.asList("Alex", "Jean", "Emily"); String str = String.join(", ", names); System.out.println(str);
Output:
Alex, Jean, Emily
If the list contains null values, “null” is displayed as a string. If you want to prevent this, you should filter the list first.
[st_adsense]
How To Join List String With Commas Using Stream API Collectors.joining() In Java 8
If there is no list of strings but other objects, these objects can be elegantly converted into strings using map() method of a stream:
Person p1 = new Person("Alex", "Normapi"); Person p2 = new Person("Thomas", "Kumoussa"); Person p3 = new Person("Yohan", "Fincho"); List persons = Arrays.asList(p1, p2, p3); String str = persons.stream() .map(p->p.getFirstName()) .collect(Collectors.joining(", ")); System.out.println(str);
Output:
Alex, Thomas, Yohan
In this example, the map() method returns the first name for each person.
How To Join List String With Commas Without Using Join
If you are forced to use an older version of Java, and don’t want or can’t use a library like Google Guava or Apache Commons, you have to write a method yourself.
/** * * @param delimiter: String that should separate the strings * @param strings: strings that should be joined * @return the joined string */ public static String join(String delimiter, String... strings) { if (strings == null || strings.length == 0) { return ""; } else if (strings.length == 1) { return strings[0]; } else { StringBuilder sb = new StringBuilder(); sb.append(strings[0]); for (int i = 1; i < strings.length; i++) { sb.append(delimiter).append(strings[i]); } return sb.toString(); } }
If the array of strings is null or empty, an empty string is returned. It may be better not to allow null values.
If the array contains only one value, only this value is returned. If the array contains multiple values, a StringBuilder is created, to which the values and the separator are passed alternately.
[st_adsense]
How To Join List String With Commas Using Apache Commons
The StringUtils class contains a join() method, as you can see in the following example:
List myList = Arrays.asList("Alex", "Yohan", "Emily"); String str = StringUtils.join(myList, ", "); System.out.println(str);
Output:
Alex, Yohan, Emily
If you are using Java 8, however, this method has no advantage over String.join().
How To Join List String With Commas Using Google Guava
Google Guavas Joiner uses a fluent API to concatenate strings with separators:
List myList = Arrays.asList("Alex", "Yohan", "Emily"); String str = Joiner.on(", ").join(myList); System.out.println(str);
Output:
Alex, Yohan, Emily
However, the above example throws an exception if names contains a null value. To prevent this, these can be explicitly excluded:
List myList = Arrays.asList("Alex", "Yohan", null, "Emily"); str = Joiner.on(", ").skipNulls().join(myList); System.out.println(str);
Output:
Alex, Yohan, Emily
With skipNulls() the null values are simply skipped.
However, if you don’t want to do that, but instead display a different value instead of nothing, you can useForNull(), as the following example shows:
List myList = Arrays.asList("Alex", "Yohan", null, "Emily"); str = Joiner.on(", ").useForNull("X").join(myList); System.out.println(str);
Output:
Alex, Yohan, X, Emily[st_adsense]