convert ArrayList.toString() back to ArrayList in one call

送分小仙女□ 提交于 2019-12-29 06:15:07

问题


I have a toString() representation of an ArrayList.

Copying the toString() value to clipboard, I want to copy it back into my IDE editor, and create the ArrayList instance in one line. In fact, what I'm really doing is this:

  • my ArrayList.toString() has data I need to setup a unit test.
  • I want to copy this ArrayList.toString() into my editor to build a test against this edge case
  • I don't want to parse anything by hand

My input looks like this:

[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]

The following do not work:

  • Arrays.asList()
  • google collections Lists.newArrayList()

Suggestions?


回答1:


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.




回答2:


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.




回答3:


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());



回答4:


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(...) ?



回答5:


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.




回答6:


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.



来源:https://stackoverflow.com/questions/2774142/convert-arraylist-tostring-back-to-arraylist-in-one-call

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!