Java program to convert a list of string to comma separated string



A list is a collection in Java which is used for storing group of elements. It is an ordered collection which also allows to store duplicate elements.

A String is a sequence of characters which is used for representing text in Java.

Here, in this article, we are given a list of strings, we have to convert it into a comma-separated string in Java using different approaches. To understand this problem, consider the following input-output scenarios:

Scenario 1

Input:
["Apple", "Banana", "Mango", "Orange"]

Output:
Apple, Banana, Mango, Orange

Scenario 2

Input:
["Red", "Green", "Blue"]

Output:
Red, Green, Blue

Scenario 3

Input:
[]

Output:

Approaches to Convert a List of Strings to a Comma-Separated String in Java

The following are the approaches to convert a list of strings into a comma-separated string in Java:

  • Using For Loop and StringBuilder
  • Using String.join()
  • Using Streams API

Using For Loop and StringBuilder

In the naive way, we will use a loop for iteratinh through the list and append each element to a StringBuilder. We will also add a comma after each element except the last one.

Example

In the following example, we will create a list of strings and then convert it to a comma-separated string using a for loop and StringBuilder:

Open Compiler
import java.util.ArrayList; import java.util.List; public class ListToCommaSeparatedString { public static void main(String[] args) { List<String> list = new ArrayList<>(); list.add("Java"); list.add("Python"); list.add("JavaScript"); list.add("C++"); list.add("Swift"); System.out.println("Original List: " + list); StringBuilder sb = new StringBuilder(); for (int i = 0; i < list.size(); i++) { sb.append(list.get(i)); if (i < list.size() - 1) { sb.append(", "); } } String commaSeparatedString = sb.toString(); System.out.println("Comma-Separated String: " + commaSeparatedString); } }

Output

Following is the output of the above code:

Original List: [Java, Python, JavaScript, C++, Swift]
Comma-Separated String: Java, Python, JavaScript, C++, Swift

Using String.join()

We also have a method of the String class called join() which can be used to join elements of a list into a single string with a specified delimiter. We will use this method to convert a list of strings to a comma-separated string.

Example

In the following example, we will create a list of strings and then convert it to a comma-separated string using the String.join() method:

Open Compiler
import java.util.ArrayList; import java.util.List; public class ListToCommaSeparatedString{ public static void main(String[] args) { List<String> list = new ArrayList<>(); list.add("Java"); list.add("Python"); list.add("JavaScript"); list.add("C++"); list.add("Swift"); System.out.println("Original List: " + list); String commaSeparatedString = String.join(", ", list); System.out.println("Comma-Separated String: " + commaSeparatedString); } }

Output

Following is the output of the above code:

Original List: [Java, Python, JavaScript, C++, Swift]
Comma-Separated String: Java, Python, JavaScript, C++, Swift

Using Streams API

In Java 8 and later, we can use the method Collectors.joining() from the Streams API to convert a list of strings to a comma-separated string. This method allows us to specify a delimiter or a prefix and suffix for the resulting string.

Example

In the following example, we will create a list of strings and then convert it to a comma-separated string using the Streams API:

Open Compiler
import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; public class ListToCommaSeparatedString { public static void main(String[] args) { List<String> list = new ArrayList<>(); list.add("Java"); list.add("Python"); list.add("JavaScript"); list.add("C++"); list.add("Swift"); System.out.println("Original List: " + list); String commaSeparatedString = list.stream() .collect(Collectors.joining(", ")); System.out.println("Comma-Separated String: " + commaSeparatedString); } }

Output

Following is the output of the above code:

Original List: [Java, Python, JavaScript, C++, Swift]
Comma-Separated String: Java, Python, JavaScript, C++, Swift
Aishwarya Naglot
Aishwarya Naglot

Writing clean code… when the bugs aren’t looking.

Updated on: 2025-08-26T16:44:22+05:30

828 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements