Java8几种常用字符串拼接方法总结

字符串的拼接在Java开发过程中经常被使用,Java中提供了6种常用的字符串拼接方法,本文主要介绍这几种拼接方法的使用
1、使用"+"号

public static void main(String[] args) {
    
    
    String str1 = "Hello";
    String str2 = "World";
    long startTimes = Instant.now().toEpochMilli();
    System.out.println("开始时间:" + startTimes);
    for (int i = 0; i < 100000; i++) {
    
    
        String str = str1 + "," + str2;
    }
    long endTimes = Instant.now().toEpochMilli();
    System.out.println("结束时间:" + endTimes);
    System.out.println("使用+执行100000次字符串拼接耗时为:" + (endTimes - startTimes) + "毫秒");
}

执行结果

开始时间:1610177104871
结束时间:1610177104916
使用+执行100000次字符串拼接耗时为:45毫秒
开始时间:1610177903188
结束时间:1610177903195
使用+执行10000次字符串拼接耗时为:7毫秒

2、使用concat

public static void main(String[] args) {
    
    
    String str1 = "Hello";
    String str2 = "World";
    long startTimes = Instant.now().toEpochMilli();
    System.out.println("开始时间:" + startTimes);
    for (int i = 0; i < 100000; i++) {
    
    
        String str = str1.concat(",").concat(str2);
    }
    long endTimes = Instant.now().toEpochMilli();
    System.out.println("结束时间:" + endTimes);
    System.out.println("使用concat执行100000次字符串拼接耗时为:" + (endTimes - startTimes) + "毫秒");
}

执行结果

开始时间:1610177258860
结束时间:1610177258916
使用concat执行100000次字符串拼接耗时为:56毫秒
开始时间:1610177958826
结束时间:1610177958832
使用concat执行10000次字符串拼接耗时为:6毫秒

3、使用StringBuffer

public static void main(String[] args) {
    
    
    String str1 = "Hello";
    String str2 = "World";
    StringBuffer stringBuffer = new StringBuffer();
    long startTimes = Instant.now().toEpochMilli();
    System.out.println("开始时间:" + startTimes);
    for (int i = 0; i < 100000; i++) {
    
    
        stringBuffer.append(str1);
        stringBuffer.append(",");
        stringBuffer.append(str2);
    }
    long endTimes = Instant.now().toEpochMilli();
    System.out.println("结束时间:" + endTimes);
    System.out.println("使用StringBuffer执行100000次字符串拼接耗时为:" + (endTimes - startTimes) + "毫秒");
}

执行结果

开始时间:1610177353095
结束时间:1610177353125
使用StringBuffer执行100000次字符串拼接耗时为:30毫秒
开始时间:1610178077641
结束时间:1610178077647
使用StringBuffer执行10000次字符串拼接耗时为:6毫秒

4、使用StringBuilder

public static void main(String[] args) {
    
    
   String str1 = "Hello";
    String str2 = "World";
    StringBuilder stringBuilder = new StringBuilder();
    long startTimes = Instant.now().toEpochMilli();
    System.out.println("开始时间:" + startTimes);
    for (int i = 0; i < 100000; i++) {
    
    
        stringBuilder.append(str1);
        stringBuilder.append(",");
        stringBuilder.append(str2);
    }
    long endTimes = Instant.now().toEpochMilli();
    System.out.println("结束时间:" + endTimes);
    System.out.println("使用StringBuilder执行100000次字符串拼接耗时为:" + (endTimes - startTimes) + "毫秒");
}

执行结果

开始时间:1610177416041
结束时间:1610177416071
使用StringBuilder执行100000次字符串拼接耗时为:30毫秒
开始时间:1610178027855
结束时间:1610178027860
使用StringBuilder执行10000次字符串拼接耗时为:5毫秒

5、使用String.join

public static void main(String[] args) {
    
    
    String str1 = "Hello";
    String str2 = "World";
    long startTimes = Instant.now().toEpochMilli();
    System.out.println("开始时间:" + startTimes);
    for (int i = 0; i < 100000; i++) {
    
    
        String.join(",", str1, str2);
    }
    long endTimes = Instant.now().toEpochMilli();
    System.out.println("结束时间:" + endTimes);
    System.out.println("使用join执行100000次字符串拼接耗时为:" + (endTimes - startTimes) + "毫秒");
}

执行结果

开始时间:1610177523763
结束时间:1610177523864
使用join执行100000次字符串拼接耗时为:101毫秒
开始时间:1610178127784
结束时间:1610178127842
使用join执行10000次字符串拼接耗时为:58毫秒

6、使用StringJoiner

public static void main(String[] args) {
    
    
    String str1 = "Hello";
    String str2 = "World";
    StringJoiner stringJoiner = new StringJoiner(",");
    long startTimes = Instant.now().toEpochMilli();
    System.out.println("开始时间:" + startTimes);
    for (int i = 0; i < 100000; i++) {
    
    
        stringJoiner.add(str1);
        stringJoiner.add(str2);
    }
    long endTimes = Instant.now().toEpochMilli();
    System.out.println("结束时间:" + endTimes);
    System.out.println("使用StringJoiner执行100000次字符串拼接耗时为:" + (endTimes - startTimes) + "毫秒");
}

执行结果

开始时间:1610177674666
结束时间:1610177674711
使用StringJoiner执行100000次字符串拼接耗时为:45毫秒
开始时间:1610177846418
结束时间:1610177846426
使用StringJoiner执行10000次字符串拼接耗时为:8毫秒

总结:使用"+"连接字符串时,使用javap -c 反编译后,发现编译器自动引入了StringBuilder类,在大规模场景中,为连接n个字符串重复地使用字符串连接操作符,需要n的平方级的时间。这是由于字符串不可变而导致的结果,当字符串连接在一起时,内容都要被拷贝;String.join的底层调用了StringJoiner,源码如下:

public static String join(CharSequence delimiter, CharSequence... elements) {
    
    
    Objects.requireNonNull(delimiter);
    Objects.requireNonNull(elements);
    // Number of elements not likely worth Arrays.stream overhead.
    StringJoiner joiner = new StringJoiner(delimiter);
    for (CharSequence cs: elements) {
    
    
        joiner.add(cs);
    }
    return joiner.toString();
}

再进入StringJoiner源码,底层使用了StringBuilder,源码如下:

private StringBuilder value;
public StringJoiner add(CharSequence newElement) {
    
    
    prepareBuilder().append(newElement);
    return this;
}
private StringBuilder prepareBuilder() {
    
    
    if (value != null) {
    
    
        value.append(delimiter);
    } else {
    
    
        value = new StringBuilder().append(prefix);
    }
    return value;
}

所以在字符串拼接时,不考虑多线程情况下,建议直接使用StringBuilder,同继承于AbstractStringBuilder的StringBuffer使用了synchronized,保证了线程的安全,多线程情况下使用StringBuffer,源码如下:

public synchronized StringBuffer append(String str) {
    
    
    toStringCache = null;
    super.append(str);
    return this;
}

猜你喜欢

转载自blog.csdn.net/liu320yj/article/details/112392384