A String can be converted into a List<Character> in Java using different approaches. We can use Java Streams, a loop with charAt(), or create a custom List using AbstractList.
- charAt() with ArrayList is a simple and easy-to-understand approach.
- Streams provide a concise way to convert the characters into a List<Character>.
Example:
Input: String = "Java"
Output: [J, a, v, a]
Ways to Convert a String to a List of Characters
1. Using String.chars() and Streams
The chars() method returns an IntStream containing the characters of the String. We can convert each integer value to a Character and collect the result into a List.
Approach:
- Create a String.
- Use chars() to get the characters as an IntStream.
- Convert each value to Character using mapToObj().
- Collect the characters into a List.
import java.util.List;
import java.util.stream.Collectors;
class StringToListStream {
public static void main(String[] args)
{
String str = "Java";
List<Character> list = str.chars().mapToObj(c -> (char)c).collect(Collectors.toList());
System.out.println("String: " + str);
System.out.println("List: " + list);
}
}
Output
String: Java List: [J, a, v, a]
Explanation: The chars() method provides the characters as integer values. mapToObj() converts them into Character objects, which are then collected into a List.
2. Using charAt() Method with a Loop
The charAt() method can be used to access each character at a particular index. Each character is added to an ArrayList.
Approach:
- Create an empty ArrayList.
- Traverse the String using a loop.
- Get each character using charAt().
- Add the character to the List.
import java.util.ArrayList;
import java.util.List;
class StringToListLoop {
public static void main(String[] args)
{
String str = "Hello";
List<Character> list = new ArrayList<>();
for (int i = 0; i < str.length(); i++) {
list.add(str.charAt(i));
}
System.out.println("String: " + str);
System.out.println("List: " + list);
}
}
Output
String: Hello List: [H, e, l, l, o]
Explanation: The loop visits each index of the String and charAt(i) retrieves the character at that position. The characters are then added to the ArrayList.
3. Using AbstractList
AbstractList can be used to create a custom List that accesses characters directly from the String. In this approach, the get() and size() methods are overridden.
Approach:
- Pass the String to a method.
- Create an AbstractList implementation.
- Use charAt() inside the get() method.
- Return the String length from the size() method.
import java.util.AbstractList;
import java.util.List;
class StringToListAbstract {
static List<Character> convertToList(String str)
{
return new AbstractList<Character>() {
@Override public Character get(int index)
{
return str.charAt(index);
}
@Override public int size()
{
return str.length();
}
};
}
public static void main(String[] args)
{
String str = "Code";
List<Character> list = convertToList(str);
System.out.println("String: " + str);
System.out.println("List: " + list);
}
}
Output
String: Code List: [C, o, d, e]
Explanation: The custom List uses the original String as its data source. The get() method returns the character at the requested index, while size() returns the length of the String. No separate collection is created to store the characters.
Note: This approach creates a view over the String rather than copying all characters into a separate ArrayList. It is useful when you want List-style access without creating another collection.