+1  A: 

this works, but perhaps someone has something more elegant?

    List<String> list = Lists.newArrayList(Splitter.on(",").omitEmptyStrings().split("JUN10, SEP10, DEC10, MAR11, JUN11, SEP11, DEC11, MAR12, JUN12, SEP12, DEC12, MAR13, DEC13, DEC14"));
    assertEquals(14, list.size());
dotnetnewbie
had to remove the brackets from beginning and ending ArrayList.toString()
dotnetnewbie
Looks like you are using Spltter from guava-libraries
harschware
yep - Lists.newArrayList is from google too
dotnetnewbie
+2  A: 

Substring the braces away, split it on , (comma and space) and finally feed it to Arrays#asList().

 String s = "[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]";
 List<String> list = Arrays.asList(s.substring(1, s.length() - 1).split(", "));

Note that this will work in your particular case, but not in all circumstances. You may for example have a list of strings of which at least one contains a subsequent comma and space. The split would then fail.

BalusC
I think you want s.length()-1, as the character at the endIndex is not included in the substring
Ophidian
Oops, the `endIndex` is indeed exclusive.
BalusC
just a matter of personal preference, but I prefer not to parse and like the google splitter solution better.
dotnetnewbie
Your choice. I just wanted to prove that it *works*. You namely mentioned that it "do not work". Since you already gave the answer using Google Collections yourself beforehand, I did not want to waste time to duplicate it.
BalusC
thanks. just to clarify - i was looking for the path of least resistance / fastest way as i just needed data for a unit test. i agree w/ parsing i could have done it...but i really wanted how can i copy ArrayList.toString() straight into my test case and have it work w/ no additional mod's. i added my google coll solution as i came up w/ it a minute after posting. thx again
dotnetnewbie
+1  A: 

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.

Joachim Sauer
A: 

I want to copy this ArrayList.toString() into my editor to build a test against this edge case

If at all you are copying, can't you just copy values omitting the square brackets and call an

Arrays.asList(...) ?
ring bearer
yep - i amended. sorry. values of type Number work fine; but what about when your list has string elements?
dotnetnewbie
@dotnetnewbie: String elements will ony ever work if none of the String elements contains the substring `", "`.
Joachim Sauer
the point of he exercise is to copy and paste into unit test and get unit test working w/ least path of resistance assuming you have a ArrayList.toString() that you can copy to clipboard from IDE
dotnetnewbie
A: 

If you are just looking for a copy paste solution,

    Arrays.asList(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);

i.e. delete the quotes and square brackets before trying to put it into asList(). That should give you a List which you could easily use to create a new ArrayList.

If the data are more complicated than doubles, you may have to find a way to parse them.

ILMTitan
P.S. data is the singular form of data.
ILMTitan
Hi ILMTitan, I like this, but it doesn't work when the List has String elements ie "[x,y,z]"
dotnetnewbie
A: 

The "must be one line" requirement pretty much ensures that you're going to get some ugly mess of chained API calls unless you write a helper method to encapsulate things.

It's kind of ugly, but similar to dotnetnewbie's approach:

List<String> result = Arrays.asList(listString.substring(1, listString.length()-1).split(",\\s*"));

It's off the cuff so I'm sure that can be cleaned up a fair bit.

A more elegant and easier to read approach would definitely be to either break it out over a couple of lines or refactor to a helper method in your test case (or a Util class) that you can call with your string and have it return the output that you'd like.

Ophidian