Open In App
Related Articles

Convert a List of String to a comma separated String in Java

Improve Article
Save Article
Like Article

Given a List of String, the task is to convert the List to a comma separated String in Java. 


Input: List<String> = ["Geeks", "ForGeeks", "GeeksForGeeks"]
Output: "Geeks, For, Geeks"

Input: List<String> = ["G", "e", "e", "k", "s"]
Output: "G, e, e, k, s"

Approach: This can be achieved with the help of join() method of String as follows.

  1. Get the List of String.
  2. Form a comma separated String from the List of String using join() method by passing comma ‘, ‘ and the list as parameters.
  3. Print the String.

Below is the implementation of the above approach: 



// Java program to convert List of String
// to comma separated String
import java.util.*;
public class GFG {
    public static void main(String args[])
        // Get the List of String
            list = new ArrayList<>(
        // Print the List of String
        System.out.println("List of String: " + list);
        // Convert the List of String to String
        String string = String.join(",", list);
        // Print the comma separated String
        System.out.println("Comma separated String: "
                           + string);


List of String: [Geeks, ForGeeks, GeeksForGeeks]
Comma separated String: Geeks,ForGeeks,GeeksForGeeks
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 21 Oct, 2022
Like Article
Save Article
Similar Reads
Complete Tutorials