Search code examples
javastringarraylistconverters

How to convert a String into an ArrayList?


In my String, I can have an arbitrary number of words which are comma separated. I wanted each word added into an ArrayList. E.g.:

String s = "a,b,c,d,e,.........";

Solution

  • Try something like

    List<String> myList = new ArrayList<String>(Arrays.asList(s.split(",")));
    

    Demo:

    String s = "lorem,ipsum,dolor,sit,amet";
    
    List<String> myList = new ArrayList<String>(Arrays.asList(s.split(",")));
    
    System.out.println(myList);  // prints [lorem, ipsum, dolor, sit, amet]