Open In App
Related Articles

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

Improve Article
Save Article
Like Article

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


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 = new HashSet<>(
        // 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);


Set of String: [ForGeeks, Geeks, GeeksForGeeks]
Comma separated String: ForGeeks, Geeks, GeeksForGeeks
Feeling lost in the vast world of Backend Development? It's time for a change! Join our Java Backend Development - Live Course and embark on an exciting journey to master backend development efficiently and on schedule.
What We Offer:
  • Comprehensive Course
  • Expert Guidance for Efficient Learning
  • Hands-on Experience with Real-world Projects
  • Proven Track Record with 100,000+ Successful Geeks

Last Updated : 11 Dec, 2018
Like Article
Save Article
Similar Reads
Complete Tutorials