java String.join() 与其他字符串连接操作

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

String.join() vs other string concatenation operations

javastringjava-8

提问by Ankur Mahajan

I have quickly read over the Java8 String apidocumentation.

我已经快速阅读了 Java8 String api文档。

Now I am little curious about String.join() method to concat/join strings.

现在我对连接/连接字符串的 String.join() 方法有点好奇。

This kind of example has helped me to understand better, though:

不过,这种例子帮助我更好地理解:

//Old way:
String str1 = "John";
String str2 = "Doe";
String result = str1 + " " +str2;
//or by using str1.concat(str2);

//New way:
String result = String.join(" ", str1, str2);

Still, I don't understand which one should I use. Is there any performance or other differences between these two processes.

不过,我不明白我应该使用哪个。这两个过程之间是否有任何性能或其他差异。

Any help would be greatly appreciated.

任何帮助将不胜感激。

回答by RealSkeptic

String.joinrelies on the class StringJoinerwhich itself relies on an internal StringBuilderto build the joined string.

String.join依赖于StringJoiner本身依赖于内部StringBuilder来构建连接字符串的类。

So performance-wise it's much the same as using a StringBuilderand appending to it, or using a chain of +(which nowadays are converted to StringBuilderoperations by the compiler).

因此,在性能方面,它与使用 aStringBuilder并附加到它或使用 a 链+(如今StringBuilder已由编译器转换为操作)非常相似。

But the significance of String.joinis not as a general replacement for +or String.concat, but in being the "reverse operation" of a String.splitoperation. It makes more sense in that context - when you have a bunch of strings that you want to join together using a delimiter - than as a replacement for concat.

但 的意义String.join不是作为+or的一般替代String.concat,而是作为一个String.split操作的“逆向操作” 。在这种情况下,它更有意义——当你有一堆字符串想要使用分隔符连接在一起时——而不是作为concat.

That is, to build an output like "a/b/c/d"or "(a+b+c+d)"when you have a,b,cand din an array or a list, String.joinor a StringJoinerwould make the operation clear and readable.

也就是说,在数组或列表中构建类似"a/b/c/d""(a+b+c+d)"当您有a, b, cand的输出dString.join或 aStringJoiner将使操作清晰易读。

回答by Roberto Attias

str1 + " " + str2is internally converted into:

str1 + " " + str2内部转换为:

StringBuffer tmp1 = new StringBuffer();
tmp1.append(str1);
tmp1.append(" ");
String tmp2 = tmp1.toString();
StringBuffer tmp3 = new StringBuffer();
tmp3.append(tmp2);
tmp3.append(str2);
String result = tmp3.toString();

str1.concat(str2)will not produce the same result, as the space wont be present between the two strings.

str1.concat(str2)不会产生相同的结果,因为两个字符串之间不会出现空格。

join should be equivalent to

加入应该相当于

StringBuffer tmp1 = new StringBuffer();
tmp1.append(str1);
tmp1.append(" ");
tmp1.append(str2);
String result = tmp.toString();

and hence be faster.

因此会更快。

回答by Rajeev Ranjan

We can use StringJoiner (Java8+)

我们可以使用 StringJoiner (Java8+)

import java.util.StringJoiner;

public class StringJoinerTest {

public static final String FILESEPARATOR = ", ";

public static void main(String[] args) {

    StringJoiner sj = new StringJoiner(FILESEPARATOR);
    sj.add("Test1");
    sj.add("Test2");
    sj.add("Test3");
    System.out.println(sj);
   }
}

Output is

输出是

Test1, Test2, Test3