Join List Strings with Commas in Java

This Java example concatenates elements of a list with commas using String.join() method.

Join List Strings with Commas in Java

In below example, we are passing a list as an argument to the String.join() method.
import java.util.Arrays;
import java.util.List;

public class Java8StringJoinExample {

    public static void main(String[] args) {

        List<String> list = Arrays.asList("Today", "i", "am", "going", "to", "office");
        String joined = String.join(",", list); 
        
        System.out.println(joined);
    }
}
Output:
Today,i,am,going,to,office
The elements of the list are joined with a single space character
String joined = String.join(" ", list); 

Comments