Convert comma separated string to ArrayList in Java example shows how to convert comma separated string to ArrayList in Java using regular expression (regex) and Java 8 stream.
How to convert comma separated string to ArrayList in Java?
1) Using the split method and regular expression (regex)
Split string using split method and then convert resulting array to List using the asList
method of the Arrays class as given below.
1 2 3 4 5 6 7 8 9 10 |
String strValues = "Java,C,C++,JavaScript"; //split the string by comma String[] strParts = strValues.split(","); ArrayList<String> aList = new ArrayList<String>( Arrays.asList(strParts) ); for(String str : aList) System.out.println(str); |
Output
1 2 3 4 |
Java C C++ JavaScript |
If the values contain space between them along with comma, List elements will have them too. In order to remove the spaces, you can use the "\\s*,\\s*"
regular expression pattern where,
1 2 3 4 5 |
\\s - a space character * - zero or more times , - followed by comma \\s - followed by space character * - zero or more times |
Here is the example program to remove the spaces while splitting the string and converting it to the ArrayList.
1 2 3 4 5 6 7 8 9 10 |
String strValues = "Java, C, C++, JavaScript"; //split the string by comma String[] strParts = strValues.split("\\s*,\\s*"); ArrayList<String> aList = new ArrayList<String>( Arrays.asList(strParts) ); for(String str : aList) System.out.println(str); |
Output
1 2 3 4 |
Java C C++ JavaScript |
2) Using the Java 8 stream
If you are using Java 8, you can use a stream as given below.
1 2 3 4 5 6 7 |
String strValues = "Java,C,C++,JavaScript"; List<String> list = Stream.of( strValues.split("\\s*,\\s*") ).collect( Collectors.toList() ); for(String str : list) System.out.println(str); |
Output
1 2 3 4 |
Java C C++ JavaScript |
Alternatively, you can also use the Pattern class as given below.
1 2 3 4 5 6 7 |
String strValues = "Java,C,C++,JavaScript"; List<String> list = Pattern.compile("\\s*,\\s*").splitAsStream(strValues).collect(Collectors.toList()); for(String str : list) System.out.println(str); |
Output
1 2 3 4 |
Java C C++ JavaScript |
How to convert comma separated string containing numbers to ArrayList of Long?
a) Using the split and parseLong methods
You can use the split
method to split comma separated values to an array of String. Iterate array, convert elements to Long using the parseLong
method and add them to ArrayList one by one as given below.
1 2 3 4 5 6 7 8 9 10 |
String strValues = "12312,23423,34311,434212"; ArrayList<Long> aList = new ArrayList<Long>(); String[] strParts = strValues.split("\\s*,\\s*"); for(String part : strParts) aList.add(Long.parseLong(part)); for(Long longValue : aList) System.out.println(longValue); |
Output
1 2 3 4 |
12312 23423 34311 434212 |
b) Using Java 8 stream
You can also use Java 8 stream as given below.
1 2 3 4 5 6 7 |
String strValues = "12312,23423,34311,434212"; List<Long> list = Stream.of(strValues.split(",")).map(Long::parseLong).collect(Collectors.toList()); for(Long longValue : list) System.out.println(longValue); |
Output
1 2 3 4 |
12312 23423 34311 434212 |
This example is a part of the Java String tutorial.
Please let me know your views in the comments section below.