This example shows how to convert HashSet to a comma separated string in Java. This example also shows how to convert Set<String> to String using various methods.
How to convert HashSet to comma separated String in Java?
There are several ways using which we can convert Set<String> to comma separated string in Java.
1. Using the join method of the String class (Java 8 and later versions)
The join
method of the String class can be used for this conversion.
1 |
public static String join(CharSequence delimiter, Iterable<? extends CharSequence> elements) |
The join
method returns a new String object made from the specified elements joined by the specified delimiter. This method is only available from Java version 8. If you are using an older version of Java, the join
method is not available and you need to use other approaches given in this example.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
import java.util.HashSet; import java.util.Set; public class HashSetToStringExample { public static void main(String[] args) { Set<String> setNames = new HashSet<String>(); setNames.add("John"); setNames.add("Maria"); setNames.add("Ryan"); setNames.add("Emily"); setNames.add("Sheila"); /* * To convert HashSet<String> to comma-separated string, use * the join method */ String strNames = String.join(",", setNames); System.out.println(strNames); } } |
Output
1 |
Emily,Ryan,Sheila,John,Maria |
2. Using Apache Commons
If you are using the Apache Commons library, you can use the join
method of the StringUtils class.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
Set<String> setNames = new HashSet<String>(); setNames.add("John"); setNames.add("Maria"); setNames.add("Ryan"); setNames.add("Emily"); setNames.add("Sheila"); /* * Use the join method of the StringUtils class */ String strNames = StringUtils.join(setNames, ","); System.out.println(strNames); |
Output
1 |
Emily,Ryan,Sheila,John,Maria |
3. Using the toString method
The toString
method of the HashSet class returns a string representation of all the elements of the HashSet in “[element 1, element 2,….element n]” format.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
Set<String> setNames = new HashSet<String>(); setNames.add("John"); setNames.add("Maria"); setNames.add("Ryan"); setNames.add("Emily"); setNames.add("Sheila"); /* * Use the toString method */ String strNames = setNames.toString(); System.out.println(strNames); |
Output
1 |
[Emily, Ryan, Sheila, John, Maria] |
If you want to remove the enclosing square brackets and spaces, you can remove them by using below given regular expression along with the String replaceAll method.
1 2 3 |
String strNames = setNames.toString().replaceAll("\\[|\\]|\\s", ""); System.out.println(strNames); |
Output
1 |
Emily,Ryan,Sheila,John,Maria |
4. Using the Iterator and StringBuilder
This approach does not use any built-in methods to convert Set<String> to a comma-separated string. Instead, it uses the Iterator to iterate through HashSet elements and append them to the StringBuilder object one by one.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
Set<String> setNames = new HashSet<String>(); setNames.add("John"); setNames.add("Maria"); setNames.add("Ryan"); setNames.add("Emily"); setNames.add("Sheila"); StringBuilder sbldNames = new StringBuilder(""); String strNames = ""; if( setNames != null && setNames.size() > 0 ){ //get an Iterator Iterator<String> iterator = setNames.iterator(); //iterate through elements while( iterator.hasNext() ){ sbldNames.append(iterator.next()).append(","); } //convert StringBuilder to String strNames = sbldNames.toString(); //remove last comma strNames = strNames.substring(0, strNames.length()-1); } System.out.println(strNames); |
Output
1 |
Emily,Ryan,Sheila,John,Maria |
Note: The StringBuilder class was introduced in Java version 1.5. If you are using Java 1.4 or older version, you can use the StringBuffer class instead.
In addition to these approaches, if you are using Google’s Guava library in your project, you can also use the Joiner class to convert HashSet to comma separated string.
Please also visit how to convert comma separated string to HashSet or Set<String> example to know more.
This example is a part of the Java HashSet Tutorial with Examples.
Please let me know your views in the comments section below.
References:
Java 8 HashSet