-5

I have List which I use to convert into String:

    private String convertList(List<String> list)
    {
        String listString = "";

        for (String s : list)
        {
            listString += s + ",";
        }
        return listString;
    }

But I also want to implement the same operation backwards. I want to generate List from String using , as delimiter. How this can be implemented?

Peter Penzov
  • 1,126
  • 134
  • 430
  • 808

1 Answers1

5

Just use split to generate an array of String tokens, and Arrays.asList to generate the List :

List<String> theList = Arrays.asList(bigString.split(","));
Arnaud
  • 17,229
  • 3
  • 31
  • 44
  • 2
    Note, this will produce an unmodifiable List. You cannot add elements to or remove elements from this List. If you need it to be modifiable you will need to do: `List listOfStrings = new ArrayList<>(Arrays.asList(listString.split(",")));` – explv Jun 10 '16 at 10:03