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:
// Java program to convert String// to comma separated ListÂÂimportjava.util.*;ÂÂpublicclassGFG {   Âpublicstaticvoidmain(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);   Â}}Output:String: Geeks For Geeks Comma separated List: [Geeks, For, Geeks]
 - Creating a modifiable List:
// Java program to convert String// to comma separated ListÂÂimportjava.util.*;ÂÂpublicclassGFG {   Âpublicstaticvoidmain(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 =newArrayList<String>(               ÂArrays.asList(elements));       Â// Print the comma separated List       ÂSystem.out.println("Comma separated List: "                          Â+ list);   Â}}Output:String: Geeks For Geeks Comma separated List: [Geeks, For, Geeks]
 
