Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Convert a Set of String to a comma separated String in Java
Let us first create a set with string values −
Set<String>set = new HashSet<>(Arrays.asList("One", "Two", "Three", "Four", "Five", "Six"));
Now, convert it to a comma separated string using String.join() −
String str = String.join(", ", set);
Example
Following is the program to convert set of string to a comma separated string in Java −
import java.util.*;
public class Demo {
public static void main(String args[]) {
Set<String>set = new HashSet<>(Arrays.asList("One", "Two", "Three", "Four", "Five", "Six"));
System.out.println("Set = " + set);
String str = String.join(", ", set);
System.out.println("Comma separated String: "+ str);
}
}
Output
Set = [Five, Six, One, Four, Two, Three] Comma separated String: Five, Six, One, Four, Two, Three
Advertisements