EN
Java - convert String to List<Character>
12 points
xxxxxxxxxx
1
import java.util.List;
2
import java.util.stream.Collectors;
3
4
public class Example1 {
5
6
public static void main(String[] args) {
7
8
String text = "abc";
9
10
List<Character> list = text.chars()
11
.mapToObj(c -> (char) c)
12
.collect(Collectors.toList());
13
14
System.out.println(list); // [a, b, c]
15
}
16
}
Output:
xxxxxxxxxx
1
[a, b, c]
Note:
In Java there is more then 1 way to create stream of characters from String, take a look on this post:
xxxxxxxxxx
1
import java.util.ArrayList;
2
import java.util.List;
3
4
public class Example2 {
5
6
public static void main(String[] args) {
7
8
String text = "abc";
9
10
List<Character> list = new ArrayList<>();
11
for (char c : text.toCharArray()) {
12
list.add(c);
13
}
14
15
System.out.println(list); // [a, b, c]
16
}
17
}
Output:
xxxxxxxxxx
1
[a, b, c]