Convert List to Comma Separated String in Java

In this post, we will learn to convert a List of String to a comma separated String in Java. To convert a List of String to comma separated string in Java, we can use the join() method from the String class. Let’s see the example below:

import java.util.ArrayList;
import java.util.List;

public class StringJoinExample {
	public static void main(String[] args) {
		// Creating a list of string
		List<String> cricketers = new ArrayList<>();
		cricketers.add("Sachin Tendulkar");
		cricketers.add("Yuvaraj Singh");
		cricketers.add("Virendra Shehawag");
		cricketers.add("Virat Kohli");
		cricketers.add("MS Dhoni");

//		converting list to a comma separated string 
		String commaSeparatedString = String.join(",", cricketers);
		System.out.println(commaSeparatedString);
	}
}

The output of the above code is:

Sachin Tendulkar,Yuvaraj Singh,Virendra Shehawag,Virat Kohli,MS Dhoni

So, using the join() method from String class in Java, we are able to convert a list of strings to a comma separated string.

Sharing Is Caring:
Subscribe
Notify of
0 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments