The above statement uses join function within the String class to join the elements of the collection into the String. The elements are joint using the specified delimiter which is a “,” in this case.
final List<String> collection = new ArrayList<>(); collection.add("Hello"); collection.add("World"); final String joinedString = String.join(",", collection); System.out.println(joinedString);
The above examples joins an array list collection of Strings to create a single comma delimited string.
Integer Collection to String
The String.join function only works on String collections. However if you do happen to have a collection of Integers and need to join these integers into a single string delimited by comma, the below example shows how.
final List<Integer> intList = new ArrayList<>(); intList.add(10); intList.add(20); intList.add(30); final String joinedIntString = String.join(",", intList.stream().map(x -> "" + x).collect(Collectors.toList())); System.out.println(joinedIntString);
The .map function maps each Integer element into a String element simply by appending the Integer to an empty string.