Generally speaking the toString()
of any objects does not contain information to reproduce the original object without any further information.
In your specific case the example could be produced by many different ArrayList
instances (as well as pretty much all other List
implementations which have identical toString()
) implementations.
As an extreme example, think of an ArrayList
that contains a single element which is the String
with the content 15.82, 15.870000000000001, 15.92, 16.32, 16.32, 16.32, 16.32, 17.05, 17.05, 17.05, 17.05, 18.29, 18.29, 19.16
. That ArrayList
would produce the exact same output as your original ArrayList
. And since two different inputs produce the same output, there's no way this function can be reversed without additional information.
If, however, we have additional information, such as the content type of the original ArrayList
, then it becomes possible in some cases. If we know that all elements of the List
were of type Double
, then it's actually pretty easy:
public static List<Double> stringToList(final String input) {
String[] elements = input.substring(1, input.length() - 1).split(", ");
List<Double> result = new ArrayList<Double>(elements.length);
for (String item : elements) {
result.add(Double.valueOf(item));
}
return result;
}
Granted, it's not a one-liner, but it's not too bad.