Splitting strings in Java I wrote a function which takes in input a list of strings that is splitted by comma. Then each generated string is trimmed.

Example: 

INPUT:    [[Cat, Dog, Snake]]
OUTPUT:   Cat Dog Snake


In particular, I'm interested in removing parenthesis and commas.
This is the code I wrote, but I think it is awful and not so efficient because I use so many String and LinkedList variables.

private List&lt;String&gt; splitListOfString(LinkedList&lt;String&gt; list)
{
String mylist = list.toString(); //is made by parenthesis, I do not want
mylist = mylist.substring(2);  //delete the first 2
mylist = mylist.substring(0, mylist.length() - 2); //delete the last 2
List&lt;String&gt; thelist = Arrays.asList(mylist.split(","));
List&lt;String&gt; thelist2 = new LinkedList&lt;String&gt;();
for (String string : thelist) {
    thelist2.add(string.trim());
}
    return thelist2;
}


The line of code involving the trim function is compulsory to avoid something like " string", that is undesirable. But I do not think that this is the best way to write this code.
How would you improve this?
I'm not sure how you got the original list-within-a-list input, but have you looked into Java Streams? Here's a tutorial that may help.

It looks like you're taking in a LinkedList, turning it into a string to manually delete the [[ and ]], splitting on comma, then adding each item to a new list.

Why not do something like the following:

private static List&lt;String&gt; splitListOfString(LinkedList&lt;String&gt; list) { 
    List&lt;String&gt; myList = new LinkedList&lt;&gt;();
    list.stream().forEach(item -&gt; myList.add(item));
    return myList;
}


In the above code, you're leaving the elements in the list (instead of converting to string), retrieving each item, then adding to your output list. It may need to be modified slightly to match your input.