java 连接字符串的最有效方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36121370/
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-11-03 00:58:50  来源:igfitidea点击:

Most efficient way to concatenate Strings

javastringbuffer

提问by Derek Noble

I was under the impression that StringBufferis the fastest way to concatenate strings, but I saw thisStack Overflow post saying that concat() is the fastest method. I tried the 2 given examples in Java 1.5, 1.6 and 1.7 but I never got the results they did. My results are almost identical to this

我的印象是这StringBuffer是连接字符串的最快方法,但我看到这篇Stack Overflow 帖子说 concat() 是最快的方法。我在 Java 1.5、1.6 和 1.7 中尝试了 2 个给定的示例,但我从未得到他们所做的结果。我的结果几乎与相同

  1. Can somebody explain what I don't understand here? What is truly the fastest way to concatenate strings in Java?

  2. Is there a different answer when one seeks the fastest way to concatenate two strings and when concatenating multiple strings?

  1. 有人可以解释我在这里不明白的地方吗?在 Java 中连接字符串的真正最快方法是什么?

  2. 当人们寻求连接两个字符串的最快方法和连接多个字符串时,是否有不同的答案?

回答by Matt Timmermans

String.concatis faster than the +operator if you are concatenating twostrings... Although this can be fixed at any time and may even have been fixed in java 8 as far as I know.

String.concat+如果您连接两个字符串,则比运算符更快......尽管这可以随时修复,据我所知甚至可能已在 java 8 中修复。

The thing you missed in the first post you referenced is that the author is concatenating exactly twostrings, and the fast methods are the ones where the size of the new character array is calculated in advance as str1.length() + str2.length(), so the underlying character array only needs to be allocated once.

您在您引用的第一篇文章中遗漏的是,作者正在连接两个字符串,而快速方法是预先计算新字符数组大小的方法为 str1.length() + str2.length( ),所以底层字符数组只需要分配一次。

Using StringBuilder() without specifying the final size, which is also how +works internally, will often need to do more allocations and copying of the underlying array.

使用 StringBuilder() 而不指定最终大小(这也是+内部的工作方式)通常需要进行更多的分配和底层数组的复制。

If you need to concatenate a bunch of strings together, then you should use a StringBuilder. If it's practical, then precompute the final size so that the underlying array only needs to be allocated once.

如果您需要将一堆字符串连接在一起,那么您应该使用 StringBuilder。如果可行,则预先计算最终大小,以便底层数组只需要分配一次。

回答by Rusheel Jain

What I understood from others answer is following:

我从其他人的回答中了解到以下内容:

If you need thread safety, use StringBuffer

如果您需要线程安全,请使用 StringBuffer

If you do not need thread safety:

如果您不需要线程安全:

If strings are known before hand and for some reasons multiple time same code needs to be run, use '+' as compiler will optimize and handle it during compile time itself.

如果字符串事先已知并且由于某些原因需要多次运行相同的代码,请使用“+”,因为编译器将在编译时本身进行优化和处理。

if only two strings need to be concatenated, use concat() as it will not require StringBuilder/StringBuffer objects to be created. Credits to @nickb

如果只需要连接两个字符串,请使用 concat() 因为它不需要创建 StringBuilder/StringBuffer 对象。归功于@nickb

If multiple strings need to be concatenated, use StringBuilder.

如果需要连接多个字符串,请使用 StringBuilder。