Learn to split string by comma or space and store in array or arraylist. Use given Java program to convert string to List in Java.
1. Convert String to List Using Regular Expression
We can use regular expression "\\s*,\\s*"
to match comma in CSV string and then do Java string split with String.split() method to convert string to list.
import java.util.Arrays; import java.util.List; public class CSVSplitExample { public static void main(String[] args) { String alpha = "how, to, do, in, java"; //Remove whitespace and split by comma List<String> result = Arrays.asList(alpha.split("\\s*,\\s*")); System.out.println(result); } }
Program Output.
[how, to, do, in, java]
This resulting ArrayList is a fixed-size unmodifiable read-only list backed by the array. It means you cannot add or remove elements from this list. If you want a modifiable list then use next approach.
Notice split()
function returns string array. So you can modify above example to split string to array as well.
2. Convert CSV to Mutable ArrayList
To get the mutable arraylist, copy all elements from read-only list received from above example into a new ArrayList object.
import java.util.Arrays; import java.util.List; public class CSVSplitExample2 { public static void main(String[] args) { String alpha = "how, to, do, in, java"; //Typecast to ArrayList List<String> result = new ArrayList<String>( Arrays.asList(alpha.split("\\s*,\\s*")) ); System.out.println(result); result.add("com"); System.out.println(result); } }
[how, to, do, in, java, com]
3. Convert List to CSV String – Java 8
If we want to convert list to CSV, then we can use String.join() method provided by Java 8.
import java.util.Arrays; import java.util.List; public class JavaListToStringExample { public static void main(String[] args) { List<String> list = Arrays.asList("how", "to", "do", "in", "java"); String result = String.join("-", list); //delimited by comma System.out.println(result); String result2 = String.join(" ", list); //delimited by space System.out.println(result2); } }
Output:
Program Output.
how-to-do-in-java how to do in java
Above examples will help you to convert String to List and also convert List to String in Java.
Happy Learning !!
Read More:
Read/Write CSV file with OpenCSV
Read/Write CSV file with SuperCSV
3 examples to parse CSV files
Join String in Java 8
Keith Rust
How do you properly split the string if a comma is part of the data in one of the elements and it’s quoted?
“\”ho,w\”,\” to\”,\” do\”,\” in\”,\” ja,v\”\”a\””
This needs to return the following splits:
“ho,w”
” to”
” do”
” in”
” ja,v””a”
Keith Rust
Of course, my quote escapes are removed from the post. 😀
Martin
String result = String.join(“-“, list); //delimited by comma
the comment should be: // delimited by “-“
Colin Richardson
Output of last example is wrong
Lokesh Gupta
True. It’s corrected now. Thanks for pointing out.
Facebamm
You have a misstake in the last exempel by the out.
Join by “,”, thats mean you have between all listitems an “,”
The Output must showing like this “how,to,do,in,java”
Lokesh Gupta
True. It’s corrected now. Thanks for pointing out.