Convert a String to a List of Character in Java
This post will discuss how to convert a string to a list of characters in Java.
1. Naive solution
A naive solution is to create a new list and add elements to it using a for-each loop, as shown below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import java.util.ArrayList; import java.util.List; // Convert a string to a list of characters class Main { public static void main(String[] args) { String string = "Techie Delight"; List<Character> chars = new ArrayList<>(); for (char ch: string.toCharArray()) { chars.add(ch); } System.out.println(chars); } } |
Output:
[T, e, c, h, i, e, , D, e, l, i, g, h, t]
We can also use a simple for-loop, as shown below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import java.util.ArrayList; import java.util.List; // Convert a string to a list of characters class Main { public static void main(String[] args) { String string = "Techie Delight"; List<Character> chars = new ArrayList<>(); for (int i = 0; i < string.length(); i++) { chars.add(string.charAt(i)); } System.out.println(chars); } } |
Output:
[T, e, c, h, i, e, , D, e, l, i, g, h, t]
2. Using Java 9
We can call the chars() method on a string in Java 9 and above, which returns an IntStream. Then we convert IntStream to Stream of Character using a lambda expression and collect the Stream to a new list using a Collector.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
import java.util.List; import java.util.stream.Collectors; // Convert a string to a list of characters class Main { public static void main(String[] args) { String string = "Techie Delight"; List<Character> chars = string.chars() // IntStream .mapToObj(e -> (char)e) // Stream<Character> .collect(Collectors.toList()); System.out.println(chars); } } |
Output:
[T, e, c, h, i, e, , D, e, l, i, g, h, t]
We can also use IntStream.range() to directly access each character and add it to the list, as shown below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import java.util.List; import java.util.stream.Collectors; import java.util.stream.IntStream; // Convert a string to a list of characters class Main { public static void main(String[] args) { String string = "Techie Delight"; List<Character> chars = IntStream.range(0, string.length()) .mapToObj(string::charAt) .collect(Collectors.toList()); System.out.println(chars); } } |
Output:
[T, e, c, h, i, e, , D, e, l, i, g, h, t]
3. Using AbstractList Interface
To create an immutable list backed by the string, we can implement the AbstractList interface.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
import java.util.AbstractList; import java.util.List; // Convert a string to a list of characters class Main { public static List<Character> convert(String string) { return new AbstractList<Character>() { @Override public Character get(int index) { return string.charAt(index); } @Override public int size() { return string.length(); } }; } public static void main(String[] args) { String string = "Techie Delight"; System.out.println(convert(string)); } } |
Output:
[T, e, c, h, i, e, , D, e, l, i, g, h, t]
4. Using Guava Library
Another plausible way of converting a string to a list of Character is using some third-party library. Guava’s Chars class provides several static utility methods pertaining to char primitives. One such method is asList(), which returns a fixed-size list backed by the array of char primitives.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
import com.google.common.primitives.Chars; import java.util.List; // Convert a string to a list of characters class Main { public static void main(String[] args) { String string = "Techie Delight"; List<Character> chars = Chars.asList(string.toCharArray()); System.out.println(chars); } } |
Output:
[T, e, c, h, i, e, , D, e, l, i, g, h, t]
We can also use the Guava library Lists class, which provides static utility methods pertaining to List instances. It has the charactersOf() method that returns a view of the specified string as an immutable list of Character values.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
import com.google.common.collect.Lists; import java.util.List; // Convert a string to a list of characters class Main { public static void main(String[] args) { String string = "Techie Delight"; List<Character> chars = Lists.charactersOf(string); System.out.println(chars); } } |
Output:
[T, e, c, h, i, e, , D, e, l, i, g, h, t]
That’s all about converting a String to a List of Character in Java.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)