This example shows how to convert comma separated string to HashSet in Java. This example also shows various ways to convert comma separated string to Set<String> using various methods.
How to convert comma separated string to HashSet in Java?
There are a couple of ways using which we can convert comma separated string to HashSet object in Java as given below.
1. Using the String split method and Arrays class
First, we will split the string by comma and that will return us an array. Once we get an array, we will convert it to the List using the asList
method of the Arrays class. The List object than can be converted to the HashSet using the HashSet constructor.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
import java.util.Arrays; import java.util.HashSet; import java.util.List; public class StringToHashSetExample { public static void main(String[] args) { String str = "1,2,3,4,2,5"; //split the string by comma String[] strParts = str.split(","); //convert array to the List using asList method List<String> listParts = Arrays.asList(strParts); //create HashSet from the List using constructor HashSet<String> hsetFromString = new HashSet<String>( listParts ); System.out.println("HashSet contains: " + hsetFromString); } } |
Output
1 |
HashSet contains: [1, 2, 3, 4, 5] |
I have coded the whole process step by step for an easier explanation. However, we can also do this in a single line as given below.
1 2 3 4 5 |
String str = "1,2,3,4,2,5"; Set<String> hsetFromString = new HashSet<String>( Arrays.asList( str.split(",") ) ); System.out.println("HashSet contains: " + hsetFromString); |
Output
1 |
HashSet contains: [1, 2, 3, 4, 5] |
If the comma separated string contains spaces in addition to the commas, we need to remove them. We can do this by using the regular expression pattern “\\s*,\\s*” in the String split method. The pattern “\\s*,\\s*” means zero or more space followed by a comma followed by zero or more space character. Let’s try it out.
1 2 3 4 5 |
String str = " 1, 2, 3 , 4, 2 ,5 "; Set<String> hsetFromString = new HashSet<String>( Arrays.asList( str.split("\\s*,\\s*") ) ); System.out.println("HashSet contains: " + hsetFromString); |
Output
1 |
HashSet contains: [ 1, 2, 3, 4, 5 ] |
As we can see from the output, all the spaces between individual values are removed now, but the first and last space characters are still there. There is a simple fix to remove them by using the String trim method.
1 2 3 4 5 |
String str = " 1, 2, 3 , 4, 2 ,5 "; Set<String> hsetFromString = new HashSet<String>( Arrays.asList( str.trim().split("\\s*,\\s*") ) ); System.out.println("HashSet contains: " + hsetFromString); |
Output
1 |
HashSet contains: [1, 2, 3, 4, 5] |
Note: As we can see from the output, the string contained “2” twice but when we converted the string to Set<String> it was added only once because Set does not allow duplicate elements.
2. Using Java 8 stream
If you are using Java version 8 or later, you can also use the stream to convert comma separated string to Set<String> object as given below.
1 2 3 4 5 |
String str = " 1, 2, 3 , 4, 2 ,5 "; Set<String> set = Stream.of( str.trim().split("\\s*,\\s*") ).collect( Collectors.toSet() ); System.out.println("HashSet contains: " + set); |
Output
1 |
HashSet contains: [1, 2, 3, 4, 5] |
You can also use the Pattern class to convert comma separated string to Set<String> as given below.
1 2 3 4 5 |
String str = " 1, 2, 3 , 4, 2 ,5 "; Set<String> set = Pattern.compile("\\s*,\\s*").splitAsStream(str.trim()).collect(Collectors.toSet()); System.out.println("HashSet contains: " + set); |
Output
1 |
HashSet contains: [1, 2, 3, 4, 5] |
Please also visit how to convert HashSet to comma separated 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