一次调用将 ArrayList.toString() 转换回 ArrayList

发布于 2024-08-31 18:35:47 字数 608 浏览 7 评论 0原文

我有一个 ArrayListtoString() 表示。

toString() 值复制到剪贴板,我想将其复制回我的 IDE 编辑器,并在一行中创建 ArrayList 实例。事实上,我真正要做的是:

  • 我的 ArrayList.toString() 有我需要设置单元测试的数据。
  • 我想将此 ArrayList.toString() 复制到我的编辑器中以针对这种边缘情况构建测试
  • 我不想手动解析任何内容

我的输入如下所示:

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

以下不起作用:

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

建议?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

清醇 2024-09-07 18:35:47

子字符串 将大括号去掉,将其拆分为 ,(逗号和空格),最后将其提供给 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(", "));

请注意,这适用于您的特定情况,但不适用于所有情况。例如,您可能有一个字符串列表,其中至少一个包含后续逗号和空格。那么分裂就会失败。

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.

债姬 2024-09-07 18:35:47

一般来说,任何对象的 toString() 都不包含在没有任何进一步信息的情况下重现原始对象的信息。

在您的具体情况下,该示例可以由许多不同的 ArrayList 实例(以及几乎所有其他具有相同 toString() 的 List 实现)生成>) 实施。

作为一个极端的例子,考虑一个包含单个元素的 String ,其内容为 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。该 ArrayList 将产生与原始 ArrayList 完全相同的输出。由于两个不同的输入产生相同的输出,因此在没有附加信息的情况下无法反转该函数。

但是,如果我们有其他信息,例如原始 ArrayList 的内容类型,那么在某些情况下这是可能的。如果我们知道 List 的所有元素都是 Double 类型,那么这实际上非常简单:

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;
}

当然,这不是一句简单的话,但也不算太糟糕。

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.

晌融 2024-09-07 18:35:47

这可行,但也许有人有更优雅的东西?

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

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());
Smile简单爱 2024-09-07 18:35:47

我想复制这个
ArrayList.toString() 进入我的编辑器
针对这种边缘情况构建测试

如果您要复制,难道您不能只复制省略方括号的值并调用

Arrays.asList(...) ?

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(...) ?
单身情人 2024-09-07 18:35:47

如果您只是寻找复制粘贴解决方案,

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

即在尝试将其放入 asList() 之前删除引号和方括号。这应该会为您提供一个列表,您可以轻松地使用它来创建新的 ArrayList。

如果数据比双精度数更复杂,您可能需要找到一种方法来解析它们。

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.

千仐 2024-09-07 18:35:47

“必须是一行”的要求几乎确保了您将得到一些丑陋的混乱的链式 API 调用,除非您编写一个辅助方法来封装事物。

这有点丑陋,但与 dotnetnewbie 的方法类似:

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

它是即兴的,所以我确信可以稍微清理一下。

一种更优雅、更易于阅读的方法肯定是将其分解为几行,或者重构为测试用例(或 Util 类)中的辅助方法,您可以使用字符串调用该方法并让它返回输出你想要的。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文