Convert a String to a List of Characters in Java - GeeksforGeeks

11 Dec, 2018
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 MethodApproach: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 Charactersimport java.util.*;  // Java program to convert// a String to a List of Characters  class GFG {      // Function to convert String    // to List of Characters    public static List<Character>    convertStringToCharList(String str)    {          // Create an empty List of character        List<Character> chars = new ArrayList<>();          // For each character in the String        // add it to the List        for (char ch : str.toCharArray()) {              chars.add(ch);        }          // return the List        return chars;    }      // Driver code    public static void main(String[] args)    {          // Get the String to be converted        String str = "Geek";          // Get the List of Character        List<Character>            chars = convertStringToCharList(str);          // Print the list of characters        System.out.println(chars);    }}Output:[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 Charactersimport java.util.*;import java.util.stream.Collectors;  // Java program to convert// a String to a List of Characters  class GFG {      // Function to convert String    // to List of Characters    public static List<Character>    convertStringToCharList(String str)    {        // Create an empty List of character      List<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 List      return chars;    }      // Driver code    public static void main(String[] args)    {          // Get the String to be converted        String str = "Geek";          // Get the List of Character        List<Character>            chars = convertStringToCharList(str);          // Print the list of characters        System.out.println(chars);    }}Output:[G, e, e, k]
Using Java 8 Stream:Approach:Get the String.Use the AbstractList interface to convert the String into List of CharacterReturn the List.Below is the implementation of the above approach:import java.util.*;  // Java program to convert// a String to a List of Characters  class GFG {      // Function to convert String    // to List of Characters    public static List<Character>    convertStringToCharList(String str)    {        return new AbstractList<Character>() {              @Override            public Character get(int index)            {                return str.charAt(index);            }              @Override            public int size()            {                return str.length();            }        };    }      // Driver code    public static void main(String[] args)    {          // Get the String to be converted        String str = "Geek";          // Get the List of Character        List<Character>            chars = convertStringToCharList(str);          // Print the list of characters        System.out.println(chars);    }}Output:[G, e, e, k]

Naive MethodApproach: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 Charactersimport java.util.*;  // Java program to convert// a String to a List of Characters  class GFG {      // Function to convert String    // to List of Characters    public static List<Character>    convertStringToCharList(String str)    {          // Create an empty List of character        List<Character> chars = new ArrayList<>();          // For each character in the String        // add it to the List        for (char ch : str.toCharArray()) {              chars.add(ch);        }          // return the List        return chars;    }      // Driver code    public static void main(String[] args)    {          // Get the String to be converted        String str = "Geek";          // Get the List of Character        List<Character>            chars = convertStringToCharList(str);          // Print the list of characters        System.out.println(chars);    }}Output:[G, e, e, k]

Approach:
Get the String.Create an empty List of Characters.Add each character of String to the List.Return the List.
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 Charactersimport java.util.*;  // Java program to convert// a String to a List of Characters  class GFG {      // Function to convert String    // to List of Characters    public static List<Character>    convertStringToCharList(String str)    {          // Create an empty List of character        List<Character> chars = new ArrayList<>();          // For each character in the String        // add it to the List        for (char ch : str.toCharArray()) {              chars.add(ch);        }          // return the List        return chars;    }      // Driver code    public static void main(String[] args)    {          // Get the String to be converted        String str = "Geek";          // Get the List of Character        List<Character>            chars = convertStringToCharList(str);          // Print the list of characters        System.out.println(chars);    }}
[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 Charactersimport java.util.*;import java.util.stream.Collectors;  // Java program to convert// a String to a List of Characters  class GFG {      // Function to convert String    // to List of Characters    public static List<Character>    convertStringToCharList(String str)    {        // Create an empty List of character      List<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 List      return chars;    }      // Driver code    public static void main(String[] args)    {          // Get the String to be converted        String str = "Geek";          // Get the List of Character        List<Character>            chars = convertStringToCharList(str);          // Print the list of characters        System.out.println(chars);    }}Output:[G, e, e, k]

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.
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 Charactersimport java.util.*;import java.util.stream.Collectors;  // Java program to convert// a String to a List of Characters  class GFG {      // Function to convert String    // to List of Characters    public static List<Character>    convertStringToCharList(String str)    {        // Create an empty List of character      List<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 List      return chars;    }      // Driver code    public static void main(String[] args)    {          // Get the String to be converted        String str = "Geek";          // Get the List of Character        List<Character>            chars = convertStringToCharList(str);          // Print the list of characters        System.out.println(chars);    }}
[G, e, e, k]

Using Java 8 Stream:Approach:Get the String.Use the AbstractList interface to convert the String into List of CharacterReturn the List.Below is the implementation of the above approach:import java.util.*;  // Java program to convert// a String to a List of Characters  class GFG {      // Function to convert String    // to List of Characters    public static List<Character>    convertStringToCharList(String str)    {        return new AbstractList<Character>() {              @Override            public Character get(int index)            {                return str.charAt(index);            }              @Override            public int size()            {                return str.length();            }        };    }      // Driver code    public static void main(String[] args)    {          // Get the String to be converted        String str = "Geek";          // Get the List of Character        List<Character>            chars = convertStringToCharList(str);          // Print the list of characters        System.out.println(chars);    }}Output:[G, e, e, k]

Approach:
Get the String.Use the AbstractList interface to convert the String into List of CharacterReturn the List.
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:
import java.util.*;  // Java program to convert// a String to a List of Characters  class GFG {      // Function to convert String    // to List of Characters    public static List<Character>    convertStringToCharList(String str)    {        return new AbstractList<Character>() {              @Override            public Character get(int index)            {                return str.charAt(index);            }              @Override            public int size()            {                return str.length();            }        };    }      // Driver code    public static void main(String[] args)    {          // Get the String to be converted        String str = "Geek";          // Get the List of Character        List<Character>            chars = convertStringToCharList(str);          // Print the list of characters        System.out.println(chars);    }}
[G, e, e, k]

Java-List-Programs
java-stream
Java-Stream-programs
Java-String-Programs
Java-Strings
Picked
Technical Scripter 2018
Java
Java-Strings
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Initialize an ArrayList in Java
HashMap in Java with Examples
Interfaces in Java
ArrayList in Java
Multidimensional Arrays in Java
Stack Class in Java
Stream In Java
Singleton Class in Java
Set in Java
Overriding in Java