Convert a String to a List of Characters in Java
Given a String, the task is to convert it into a List of Characters in Java.
Examples:
Input: String = "Geeks" Output: [G, e, e, k, s] Input: String = "GeeksForGeeks" Output: [G, e, e, k, s, F, o, r, G, e, e, k, s]
Below are the various ways to do so:
- Naive Method
Approach:
- Get the String.
- Create an empty List of Characters.
- Add each character of String to the List.
- Return the List.
Below is the implementation of the above approach:
// Java program to illustrate// Converting a String to a List// of Charactersimportjava.util.*;// Java program to convert// a String to a List of CharactersclassGFG {// Function to convert String// to List of CharacterspublicstaticList<Character>convertStringToCharList(String str){// Create an empty List of characterList<Character> chars =newArrayList<>();// For each character in the String// add it to the Listfor(charch : str.toCharArray()) {chars.add(ch);}// return the Listreturnchars;}// Driver codepublicstaticvoidmain(String[] args){// Get the String to be convertedString str ="Geek";// Get the List of CharacterList<Character>chars = convertStringToCharList(str);// Print the list of charactersSystem.out.println(chars);}}chevron_rightfilter_noneOutput:[G, e, e, k]
- Using Java 8 Stream:
Approach:
- Get the String.
- Create a List of Characters.
- Convert to String to IntStream using chars() method.
- Convert IntStream to Stream
using mapToObj() method. - Collect the elements as a List Of Characters using collect()
- Return the List.
Below is the implementation of the above approach:
// Java program to illustrate// Converting a String to a List// of Charactersimportjava.util.*;importjava.util.stream.Collectors;// Java program to convert// a String to a List of CharactersclassGFG {// Function to convert String// to List of CharacterspublicstaticList<Character>convertStringToCharList(String str){// Create an empty List of characterList<Character> chars = str// Convert to String to IntStream.chars()// Convert IntStream to Stream<Character>.mapToObj(e -> (char)e)// Collect the elements as a List Of Characters.collect(Collectors.toList());// return the Listreturnchars;}// Driver codepublicstaticvoidmain(String[] args){// Get the String to be convertedString str ="Geek";// Get the List of CharacterList<Character>chars = convertStringToCharList(str);// Print the list of charactersSystem.out.println(chars);}}chevron_rightfilter_noneOutput:[G, e, e, k]
- Using Java 8 Stream:
Approach:
- Get the String.
- Use the AbstractList interface to convert the String into List of Character
- Return the List.
Below is the implementation of the above approach:
importjava.util.*;// Java program to convert// a String to a List of CharactersclassGFG {// Function to convert String// to List of CharacterspublicstaticList<Character>convertStringToCharList(String str){returnnewAbstractList<Character>() {@OverridepublicCharacter get(intindex){returnstr.charAt(index);}@Overridepublicintsize(){returnstr.length();}};}// Driver codepublicstaticvoidmain(String[] args){// Get the String to be convertedString str ="Geek";// Get the List of CharacterList<Character>chars = convertStringToCharList(str);// Print the list of charactersSystem.out.println(chars);}}chevron_rightfilter_noneOutput:[G, e, e, k]
Recommended Posts:
- Convert List of Characters to String in Java
- Program to convert List of Integer to List of String in Java
- Program to convert List of String to List of Integer in Java
- Convert a List of String to a comma separated String in Java
- Convert String into comma separated List in Java
- Convert an ArrayList of String to a String array in Java
- Convert a Set of String to a comma separated String in Java
- Convert List to Set in Java
- Program to Convert Set to List in Java
- Program to Convert List to Map in Java
- Convert an Iterator to a List in Java
- Convert Set of String to Array of String in Java
- Program to convert Array to List in Java
- Program to Convert List to Stream in Java
- Program to Convert a Vector to List in Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



