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:

Download  Run Code

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:

Download  Run Code

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.

Download  Run Code

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:

Download  Run Code

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.

Download  Run Code

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.

Download Code

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.

Download Code

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.