将 Set内容放入其中的最快方法到单个字符串,单词之间用空格分隔?
我有一些 Set
,并且希望将其中的每一个转换为单个 String
,其中原始 Set
的每个元素都被分隔开通过一个空格“”。 一个幼稚的第一个方法是像这样做
Set<String> set_1;
Set<String> set_2;
StringBuilder builder = new StringBuilder();
for (String str : set_1) {
builder.append(str).append(" ");
}
this.string_1 = builder.toString();
builder = new StringBuilder();
for (String str : set_2) {
builder.append(str).append(" ");
}
this.string_2 = builder.toString();
有人能想到更快、更漂亮或更有效的方法来做到这一点吗?
I have a few Set<String>
s and want to transform each of these into a single String
where each element of the original Set
is separated by a whitespace " ".
A naive first approach is doing it like this
Set<String> set_1;
Set<String> set_2;
StringBuilder builder = new StringBuilder();
for (String str : set_1) {
builder.append(str).append(" ");
}
this.string_1 = builder.toString();
builder = new StringBuilder();
for (String str : set_2) {
builder.append(str).append(" ");
}
this.string_2 = builder.toString();
Can anyone think of a faster, prettier or more efficient way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
使用commons/lang,您可以使用StringUtils.join:为了简洁起见,你无法真正击败它。更新:
重新阅读这个答案,我现在更喜欢有关 Guava 的 Joiner 的其他答案。事实上,这些天我不再接近 apache commons。
另一个更新:
Java 8 引入了方法
String.join()
虽然这不像 Guava 版本那么灵活,但当您不这样做时它很方便您的类路径上没有 Guava 库。
With commons/lang you can do this using StringUtils.join:You can't really beat that for brevity.Update:
Re-reading this answer, I would prefer the other answer regarding Guava's Joiner now. In fact, these days I don't go near apache commons.
Another Update:
Java 8 introduced the method
String.join()
While this isn't as flexible as the Guava version, it's handy when you don't have the Guava library on your classpath.
如果您使用的是 Java 8,则可以使用 原生
方法:
设置
实现了 Iterable,所以只需使用:If you are using Java 8, you can use the native
method:
Set
implementsIterable
, so simply use:作为 Seanizer 的 commons-lang 答案的对立面,如果您使用的是 Google 的 Guava Libraries (在很多方面,我认为它是 commons-lang 的“继承者”),您可以使用 Joiner:
具有一些辅助方法的优点,可以执行以下操作:
或者
它还支持直接附加到 StringBuilders 和 Writers 等礼貌。
As a counterpoint to Seanizer's commons-lang answer, if you're using Google's Guava Libraries (which I'd consider the 'successor' to commons-lang, in many ways), you'd use Joiner:
with the advantage of a few helper methods to do things like:
or
It also has support for appending direct to StringBuilders and Writers, and other such niceties.
也许是一个更短的解决方案:
或者
但是原生的,肯定更快
Maybe a shorter solution:
or
but native, definitely faster
我没有可用的 StringUtil 库(我别无选择),所以使用标准 Java 我想出了这个..
如果您确信您的设置数据不会包含任何逗号或方括号,您可以使用:
一组“a”、“b”、“c”通过.toString() 转换为字符串“[a,b,c]”。
然后根据需要替换多余的标点符号。
污秽。
I don't have the StringUtil library available (I have no choice over that) so using standard Java I came up with this ..
If you're confident that your set data won't include any commas or square brackets, you could use:
A set of "a", "b", "c" converts via .toString() to string "[a,b,c]".
Then replace the extra punctuation as necesary.
Filth.
我用这个方法:
I use this method:
我对代码复制感到困惑,为什么不将其分解到一个接受一组并返回一个字符串的函数中?
除此之外,我不确定您可以做很多事情,除了向字符串生成器提供有关预期容量的提示(如果您可以根据设置大小和字符串长度的合理预期来计算它)。
也有用于此目的的库函数,但我怀疑它们的效率是否明显更高。
I'm confused about the code replication, why not factor it into a function that takes one set and returns one string?
Other than that, I'm not sure that there is much that you can do, except maybe giving the stringbuilder a hint about the expected capacity (if you can calculate it based on set size and reasonable expectation of string length).
There are library functions for this as well, but I doubt they're significantly more efficient.
这可以通过从集合中创建一个流,然后使用归约操作组合元素来完成,如下所示(有关 Java 8 流的更多详细信息,请检查 此处):
This can be done by creating a stream out of the set and then combine the elements using a reduce operation as shown below (for more details about Java 8 streams check here):