Saturday, January 17, 2026
HomeLanguagesJavaConvert a Set of String to a comma separated String in Java

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

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

Examples:

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

Input: Set<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 Set of String.
  2. Form a comma separated String from the Set of String using join() method by passing comma ‘, ‘ and the set as parameters.
  3. Print the String.

Below is the implementation of the above approach:




// Java program to convert Set of String
// to comma separated String
  
import java.util.*;
  
public class GFG {
    public static void main(String args[])
    {
  
        // Get the Set of String
        Set<String>
            set = new HashSet<>(
                Arrays
                    .asList("Geeks",
                            "ForGeeks",
                            "GeeksForGeeks"));
  
        // Print the Set of String
        System.out.println("Set of String: " + set);
  
        // Convert the Set of String to String
        String string = String.join(", ", set);
  
        // Print the comma separated String
        System.out.println("Comma separated String: "
                           + string);
    }
}


Output:

Set of String: [ForGeeks, Geeks, GeeksForGeeks]
Comma separated String: ForGeeks, Geeks, GeeksForGeeks
RELATED ARTICLES

Most Popular

Dominic
32474 POSTS0 COMMENTS
Milvus
118 POSTS0 COMMENTS
Nango Kala
6846 POSTS0 COMMENTS
Nicole Veronica
11977 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12062 POSTS0 COMMENTS
Shaida Kate Naidoo
6985 POSTS0 COMMENTS
Ted Musemwa
7219 POSTS0 COMMENTS
Thapelo Manthata
6933 POSTS0 COMMENTS
Umr Jansen
6911 POSTS0 COMMENTS