string Groovy 2.x 中加入字符串集合的最佳方式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27073402/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 16:19:33  来源:igfitidea点击:

Grooviest way to join collection of Strings in Groovy 2.x

stringjoingroovy

提问by IAmYourFaja

I just tried:

我刚试过:

List<String> values = getSomehow()
values.join(",")

But see that joinhas been deprecated as of 2.1. So I ask: How should I be writing this in accordance with the latest preferred/non-deprecated syntax?

但是请注意join,从 2.1 开始,它已被弃用。所以我问:我应该如何根据最新的首选/非弃用语法编写这个?

Also, is there a way to accomplish this with closures? I feel like I could be utilizing collect()or something similar here.

另外,有没有办法用闭包来实现这一点?我觉得我可以在collect()这里使用或类似的东西。

回答by Durandal

You can use the Iteratorvariation of the joinmethod in DefaultGroovyMethods. It's signature is the same, only the separator needs to be passed in.

您可以使用Iteratorjoin方法的变体DefaultGroovyMethods。它的签名是一样的,只需要传入分隔符。

It would look like this:

它看起来像这样:

List<String> values = ["string1", "string2", "string3"]
String joinedValues = values.join(",")

Or you can do it all on one line:

或者您可以在一行上完成所有操作:

String joinedValues = ["string1", "string2", "string3"].join(",")

回答by kousen

If you're worried about the deprecation issue, the current version of the Groovy JDK (http://groovy-lang.org/gdk.html) shows a join(String)method in Iterable, Object[], and Iterator, none of which are deprecated.

如果您担心弃用问题,Groovy JDK 的当前版本 ( http://groovy-lang.org/gdk.html)join(String)在、 和 中显示了一个方法Iterable,其中没有一个被弃用。Object[]Iterator

Since all the collections implement Iterable, your original syntax was fine.

由于所有集合都实现了Iterable,您的原始语法很好。

If you really want to use a closure, then

如果你真的想使用闭包,那么

List strings = ['this', 'is', 'a', 'list']
String result = strings.inject { acc, val ->
    "$acc,$val"
}
assert result == 'this,is,a,list'

works, but it's certainly not any simpler than just strings.join(',').

有效,但肯定不会比strings.join(',').

回答by dmahapatro

You need to use the Iterableor Iterator<Object>version of join:

您需要使用IterableIterator<Object>版本join

join(Iterator<Object> self, String separator)

join(Iterator<Object> self, String separator)

instead of

代替

join(Collection self, String separator).

join(Collection self, String separator).

This is the only variety of joinwhich is deprecated.

这是唯一join不推荐使用的变体。

join(Iterable self, String separator)and join(Object[] self, String separator)are few more which are in use.

join(Iterable self, String separator)join(Object[] self, String separator)更小,其在使用中。

回答by mih0vil

As I see it in documentation, joinis deprecated In Collections, but not in Iterable.

正如我在文档中看到的那样,join在集合中不推荐使用,但在 Iterable 中不推荐使用。

def joinedValues = (values as Iterable).join ', '

Using closures you could try to write it with .inject, headand tail:

使用闭包,您可以尝试使用.inject,head和编写它tail

values.head() + (values.size() > 1 ? values.tail().inject( '' ) { acc, i -> acc+', ' + i } : '')