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:

  1. Naive Method

    Approach:

    1. Get the String.
    2. Create an empty List of Characters.
    3. Add each character of String to the List.
    4. Return the List.

    Below is the implementation of the above approach:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java program to illustrate
    // Converting a String to a List
    // of Characters
    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)
        {
      
            // 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);
        }
    }

    chevron_right

    
    

    Output:

    [G, e, e, k]
    
  2. Using Java 8 Stream:

    Approach:

    1. Get the String.
    2. Create a List of Characters.
    3. Convert to String to IntStream using chars() method.
    4. Convert IntStream to Stream using mapToObj() method.
    5. Collect the elements as a List Of Characters using collect()
    6. Return the List.

    Below is the implementation of the above approach:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java program to illustrate
    // Converting a String to a List
    // of Characters
    import 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);
        }
    }

    chevron_right

    
    

    Output:

    [G, e, e, k]
    
  3. Using Java 8 Stream:

    Approach:

    1. Get the String.
    2. Use the AbstractList interface to convert the String into List of Character
    3. Return the List.

    Below is the implementation of the above approach:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    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);
        }
    }

    chevron_right

    
    

    Output:

    [G, e, e, k]
    


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.