首页 > 应用开发 > > Java字符串拼接的五种方法,哪种性能最好?
加号 “+”
String contact() 方法
StringUtils.join() 方法
StringBuffer append() 方法
StringBuilder append() 方法

方法1 加号 “+” 拼接 和 方法2 String contact() 方法 适用于小数据量的操作,代码简洁方便,加号“+” 更符合我们的编码和阅读习惯;
方法3 StringUtils.join() 方法 适用于将ArrayList转换成字符串,就算90万条数据也只需68ms,可以省掉循环读取ArrayList的代码;
方法1 加号 “+” 拼接 和 方法2 String contact() 方法 的时间和空间成本都很高(分析在本文末尾),不能用来做批量数据的处理。
package cnblogs.twzheng.lab2;
/** * @author Tan Wenzheng * */import java.util.ArrayList;import java.util.List;
import org.apache.commons.lang3.StringUtils;
public class TestString {
private static final int max = 100;
public void testPlus() { System.out.println(">>> testPlus() <<<");
String str = "";
long start = System.currentTimeMillis();
for (int i = ; i < max; i++) { str = str + "a"; }
long end = System.currentTimeMillis();
long cost = end - start;
System.out.println(" {str + \"a\"} cost=" + cost + " ms"); }
public void testConcat() { System.out.println(">>> testConcat() <<<");
String str = "";
long start = System.currentTimeMillis();
for (int i = ; i < max; i++) { str = str.concat("a"); }
long end = System.currentTimeMillis();
long cost = end - start;
System.out.println(" {str.concat(\"a\")} cost=" + cost + " ms"); }
public void testJoin() { System.out.println(">>> testJoin() <<<");
long start = System.currentTimeMillis();
List<String> list = new ArrayList<String>();
for (int i = ; i < max; i++) { list.add("a"); }
long end1 = System.currentTimeMillis(); long cost1 = end1 - start;
StringUtils.join(list, "");
long end = System.currentTimeMillis(); long cost = end - end1;
System.out.println(" {list.add(\"a\")} cost1=" + cost1 + " ms"); System.out.println(" {StringUtils.join(list, \"\")} cost=" + cost + " ms"); }
public void testStringBuffer() { System.out.println(">>> testStringBuffer() <<<");
long start = System.currentTimeMillis();
StringBuffer strBuffer = new StringBuffer();
for (int i = ; i < max; i++) { strBuffer.append("a"); } strBuffer.toString();
long end = System.currentTimeMillis();
long cost = end - start;
System.out.println(" {strBuffer.append(\"a\")} cost=" + cost + " ms"); }
public void testStringBuilder() { System.out.println(">>> testStringBuilder() <<<");
long start = System.currentTimeMillis();
StringBuilder strBuilder = new StringBuilder();
for (int i = ; i < max; i++) { strBuilder.append("a"); } strBuilder.toString();
long end = System.currentTimeMillis();
long cost = end - start;
System.out .println(" {strBuilder.append(\"a\")} cost=" + cost + " ms"); }}
-
-
-
-
-
-
-
-
-
-
-
> testPlus() <<< {str + "a"} cost= ms> testConcat() <<< {str.concat("a")} cost= ms> testJoin() <<< {list.add("a")} cost1= ms {StringUtils.join(list, "")} cost=20 ms> testStringBuffer() <<< {strBuffer.append("a")} cost= ms> testStringBuilder() <<< {strBuilder.append("a")} cost= ms
-
-
-
-
-
-
-
-
-
-
-
> testPlus() <<< {str + "a"} cost=10 ms> testConcat() <<< {str.concat("a")} cost= ms> testJoin() <<< {list.add("a")} cost1= ms {StringUtils.join(list, "")} cost=20 ms> testStringBuffer() <<< {strBuffer.append("a")} cost= ms> testStringBuilder() <<< {strBuilder.append("a")} cost= ms
-
-
-
-
-
-
-
-
-
-
-
> testPlus() <<< {str + "a"} cost=150 ms> testConcat() <<< {str.concat("a")} cost=70 ms> testJoin() <<< {list.add("a")} cost1= ms {StringUtils.join(list, "")} cost=30 ms> testStringBuffer() <<< {strBuffer.append("a")} cost= ms> testStringBuilder() <<< {strBuilder.append("a")} cost= ms
> testPlus() <<< {str + "a"} cost=4198 ms> testConcat() <<< {str.concat("a")} cost=1862 ms> testJoin() <<< {list.add("a")} cost1=21 ms {StringUtils.join(list, "")} cost=49 ms> testStringBuffer() <<< {strBuffer.append("a")} cost=10 ms> testStringBuilder() <<< {strBuilder.append("a")} cost=10 ms
> testPlus() <<< {str + "a"} cost=17196 ms> testConcat() <<< {str.concat("a")} cost=7653 ms> testJoin() <<< {list.add("a")} cost1=20 ms {StringUtils.join(list, "")} cost=51 ms> testStringBuffer() <<< {strBuffer.append("a")} cost=20 ms> testStringBuilder() <<< {strBuilder.append("a")} cost=16 ms
> testPlus() <<< {str + "a"} cost=124693 ms> testConcat() <<< {str.concat("a")} cost=49439 ms> testJoin() <<< {list.add("a")} cost1=21 ms {StringUtils.join(list, "")} cost=50 ms> testStringBuffer() <<< {strBuffer.append("a")} cost=20 ms> testStringBuilder() <<< {strBuilder.append("a")} cost=10 ms
> testPlus() <<< {str + "a"} cost=456739 ms> testConcat() <<< {str.concat("a")} cost=186252 ms> testJoin() <<< {list.add("a")} cost1=20 ms {StringUtils.join(list, "")} cost=68 ms> testStringBuffer() <<< {strBuffer.append("a")} cost=30 ms> testStringBuilder() <<< {strBuilder.append("a")} cost=24 ms
public String concat(String str) { int otherLen = str.length(); if (otherLen == ) { return this; } int len = value.length; char buf[] = Arrays.copyOf(value, len + otherLen); str.getChars(buf, len); return new String(buf, true); }
/** * Appends the specified string to this character sequence. * <p> * The characters of the {@code String} argument are appended, in * order, increasing the length of this sequence by the length of the * argument. If {@code str} is {@code null}, then the four * characters {@code "null"} are appended. * <p> * Let <i>n</i> be the length of this character sequence just prior to * execution of the {@code append} method. Then the character at * index <i>k</i> in the new character sequence is equal to the character * at index <i>k</i> in the old character sequence, if <i>k</i> is less * than <i>n</i>; otherwise, it is equal to the character at index * <i>k-n</i> in the argument {@code str}. * * @param str a string. * @return a reference to this object. */ public AbstractStringBuilder append(String str) { if (str == null) str = "null"; int len = str.length(); ensureCapacityInternal(count + len); str.getChars(, len, value, count); count += len; return this; }
/** * This method has the same contract as ensureCapacity, but is * never synchronized. */ private void ensureCapacityInternal(int minimumCapacity) { // overflow-conscious code if (minimumCapacity - value.length > ) expandCapacity(minimumCapacity); }
/** * This implements the expansion semantics of ensureCapacity with no * size check or synchronization. */ void expandCapacity(int minimumCapacity) { int newCapacity = value.length * 2 + 2; if (newCapacity - minimumCapacity < ) newCapacity = minimumCapacity; if (newCapacity < ) { if (minimumCapacity < ) // overflow throw new OutOfMemoryError(); newCapacity = Integer.MAX_VALUE; } value = Arrays.copyOf(value, newCapacity); }
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/1678/viewspace-3134981/ ,如需转载,请注明出处,否则将追究法律责任。