EN
Java - convert String to ArrayList
6
points
In Java, we can convert String to ArrayList in following ways.
1. Split String by comma
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Example1 {
public static void main(String[] args) {
String text = "ab,cd,ef";
List<String> list = new ArrayList<>(Arrays.asList(text.split(",")));
System.out.println(list); // [ab, cd, ef]
}
}
Output:
[ab, cd, ef]
2. Split String by every letter
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Example2 {
public static void main(String[] args) {
String text = "abc";
List<String> list = new ArrayList<>(Arrays.asList(text.split("")));
System.out.println(list); // [a, b, c]
}
}
Output:
[a, b, c]