Convert String into comma separated List in Java

Given a String, the task is to convert it into comma separated List.

Examples:

Input: String = "Geeks For Geeks"
Output: List = [Geeks, For, Geeks]

Input: String = "G e e k s"
Output: List = [G, e, e, k, s]

Approach: This can be achieved by converting the String into String Array, and then creating an List from that array. However this List can be of 2 types based on their method of creation – modifiable, and unmodifiable.

  • Creating an unmodifiable List:
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java program to convert String
    // to comma separated List
      
    import java.util.*;
      
    public class GFG {
        public static void main(String args[])
        {
      
            // Get the String
            String string = "Geeks For Geeks";
      
            // Print the String
            System.out.println("String: " + string);
      
            // convert String to array of String
            String[] elements = string.split(" ");
      
            // Convert String array to List of String
            // This List is unmodifiable
            List<String> list = Arrays.asList(elements);
      
            // Print the comma separated List
            System.out.println("Comma separated List: "
                               + list);
        }
    }

    chevron_right

    
    

    Output:

    String: Geeks For Geeks
    Comma separated List: [Geeks, For, Geeks]
    
  • Creating a modifiable List:
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java program to convert String
    // to comma separated List
      
    import java.util.*;
      
    public class GFG {
        public static void main(String args[])
        {
      
            // Get the String
            String string = "Geeks For Geeks";
      
            // Print the String
            System.out.println("String: " + string);
      
            // convert String to array of String
            String[] elements = string.split(" ");
      
            // Convert String array to List of String
            // This List is modifiable
            List<String>
                list = new ArrayList<String>(
                    Arrays.asList(elements));
      
            // Print the comma separated List
            System.out.println("Comma separated List: "
                               + list);
        }
    }

    chevron_right

    
    

    Output:

    String: Geeks For Geeks
    Comma separated List: [Geeks, For, Geeks]
    


My Personal Notes arrow_drop_up

Technical Content Engineer at GeeksForGeeks

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.