Convert String to ArrayList in Java example shows how to convert String to ArrayList in Java using the split method and regular expression.
How to convert String to an ArrayList in Java?
We are going to convert string such that each word of the string will become an element of an ArrayList. We are going to do that by using the split
method of the String class as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
package com.javacodeexamples.stringexamples; import java.util.ArrayList; import java.util.Arrays; public class StringToArrayListExample { public static void main(String[] args) { String str = "I Love Java"; //split string by space String[] strParts = str.split(" "); //convert string ArrayList<String> aList = new ArrayList<String>( Arrays.asList(strParts) ); //print ArrayList for(String s : aList) System.out.println(s); } } |
Output
1 2 3 |
I Love Java |
How to convert comma separated String to an ArrayList?
If a string contains comma separated values which you want to convert to an ArrayList such that each value becomes an element of an ArrayList, use below given code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
package com.javacodeexamples.stringexamples; import java.util.ArrayList; import java.util.Arrays; public class StringToArrayListExample { public static void main(String[] args) { String str = "Convert,String,to,ArrayList,Example"; //split the string by comma String[] strParts = str.split(","); //convert string array to ArrayList ArrayList<String> aList = new ArrayList<String>( Arrays.asList(strParts) ); //print ArrayList for(String s : aList) System.out.println(s); } } |
Output
1 2 3 4 5 |
Convert String to ArrayList Example |
How to convert String to an ArrayList containing each character of the String?
1) Using charAt and valueOf methods of String class
Loop through the characters of the string and convert each character to string using the charAt
method. Finally, add each character string to an ArrayList using the add method of an ArrayList and valueOf
method of the String class as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
String str = "ArrayList"; //create new ArrayList ArrayList<String> aList = new ArrayList<String>( str.length() ); //iterate the String characters for(char ch : str.toCharArray()){ //convert each char to String and add to ArrayList aList.add( String.valueOf(ch) ); } //print ArrayList for(String s : aList) System.out.println(s); |
Output
1 2 3 4 5 6 7 8 9 |
A r r a y L i s t |
2) Using Regular Expression
Use regular expression along with the split
method of the String class to split the string by empty strings as given below.
1 2 3 4 5 6 7 8 9 10 11 |
String str = "String"; //split the string by empty string to get all the characters String[] strParts = str.split(""); //convert string array to ArrayList ArrayList<String> aList = new ArrayList<String>( Arrays.asList(strParts) ); //print ArrayList for(String s : aList) System.out.println(s); |
Output
1 2 3 4 5 6 |
S t r i n g |
Important Note:
If you are using Java 7 or below then the first element of an ArrayList is a blank or empty string. However, If you are using Java 8 or later, the first element returned from the split
method is no longer an empty String.
You can use the “(?!^)” pattern instead of an empty string to correct the problem where,
1 2 |
?! - means negative look ahead ^ - start of the string |
When used along with the split
method, the regular expression pattern “(?!^)” means that split all the characters of the string except for the start of the string (i.e. first character of the String).
1 2 3 4 5 6 7 8 9 10 11 |
String str = "String"; //split the string by empty string to get all characters String[] strParts = str.split("(?!^)"); //convert string array to ArrayList ArrayList<String> aList = new ArrayList<String>( Arrays.asList(strParts) ); //print ArrayList for(String s : aList) System.out.println(s); |
Output
1 2 3 4 5 6 |
S t r i n g |
This example is a part of the Java String tutorial and Java ArrayList tutorial
Please let me know your views in the comments section below.