【Java】Java字符串拼接的五种⽅法,哪种性能最好?
字符串拼接⼀般使⽤“+”,但是“+”不能满⾜⼤批量数据的处理,Java中有以下五种⽅法处理字符串拼接,各有优缺点,程序开发应选择合适的⽅法实现。
1. 加号 “+”
2. String contact() ⽅法
3. StringUtils.join() ⽅法
4. StringBuffer append() ⽅法
5. StringBuilder append() ⽅法
> 经过简单的程序测试,从执⾏100次到90万次的时间开销如下表:
由此可以看出:
1. ⽅法1 加号 “+” 拼接 和 ⽅法2 String contact() ⽅法 适⽤于⼩数据量的操作,代码简洁⽅便,加号“+” 更符合我们的编码和阅
读习惯;
2. ⽅法3 StringUtils.join() ⽅法 适⽤于将ArrayList转换成字符串,就算90万条数据也只需68ms,可以省掉循环读取ArrayList的代
码;
3. ⽅法4 StringBuffer append() ⽅法 和 ⽅法5 StringBuilder append() ⽅法 其实他们的本质是⼀样的,都是继承⾃
AbstractStringBuilder,效率最⾼,⼤批量的数据处理最好选择这两种⽅法。
4. ⽅法1 加号 “+” 拼接 和 ⽅法2 String contact() ⽅法 的时间和空间成本都很⾼(分析在本⽂末尾),不能⽤来做批量数据的处
理。
测试代码,供参考
package;
/**
*
*/
import ArrayList;
import List;
import 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 =0; 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 =0; i < max; i++){
str = at("a");
}
long end = System.currentTimeMillis();
long cost = end - start;
System.out.println(" {at(\"a\")} cost="+ cost +" ms"); }
public void testJoin(){
System.out.println(">>> testJoin() <<<");
long start = System.currentTimeMillis();
List<String> list =new ArrayList<String>();
for(int i =0; 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 =0; i < max; i++){
strBuffer.append("a");
}
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 =0; i < max; i++){
strBuilder.append("a");
}
long end = System.currentTimeMillis();
long cost = end - start;
System.out
.println(" {strBuilder.append(\"a\")} cost="+ cost +" ms");
}
}
测试结果
1. 执⾏100次, private static final int max = 100;
>>>testPlus()<<<
{str +"a"} cost=0 ms
>>>testConcat()<<<
{at("a")} cost=0 ms
>>>testJoin()<<<
{list.add("a")} cost1=0 ms
{StringUtils.join(list,"")} cost=20 ms
>>>testStringBuffer()<<<
{strBuffer.append("a")} cost=0 ms
>>>testStringBuilder()<<<
{strBuilder.append("a")} cost=0 ms
2. 执⾏1000次, private static final int max = 1000;
>>>testPlus()<<<
{str +"a"} cost=10 ms
>>>testConcat()<<<
{at("a")} cost=0 ms
>>>testJoin()<<<
{list.add("a")} cost1=0 ms
{StringUtils.join(list,"")} cost=20 ms
>>>testStringBuffer()<<<
{strBuffer.append("a")} cost=0 ms
>>>testStringBuilder()<<<
{strBuilder.append("a")} cost=0 ms
3. 执⾏1万次, private static final int max = 10000;
>>>testPlus()<<<
{str +"a"} cost=150 ms
>>>testConcat()<<<
{at("a")} cost=70 ms
>>>testJoin()<<<
>>>testJoin()<<<
{list.add("a")} cost1=0 ms
{StringUtils.join(list,"")} cost=30 ms
>>>testStringBuffer()<<<
{strBuffer.append("a")} cost=0 ms
>>>testStringBuilder()<<<
java的tostring方法{strBuilder.append("a")} cost=0 ms
4. 执⾏10万次, private static final int max = 100000;
>>>testPlus()<<<
{str +"a"} cost=4198 ms
>>>testConcat()<<<
{at("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
5. 执⾏20万次, private static final int max = 200000;
>>>testPlus()<<<
{str +"a"} cost=17196 ms
>>>testConcat()<<<
{at("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
6. 执⾏50万次, private static final int max = 500000;
>>>testPlus()<<<
{str +"a"} cost=124693 ms
>>>testConcat()<<<
{at("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
7. 执⾏90万次, private static final int max = 900000;
>>>testPlus()<<<
{str +"a"} cost=456739 ms
>>>testConcat()<<<
{at("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
查看源代码,以及简单分析
String contact 和 StringBuffer,StringBuilder 的源代码都可以在Java库⾥到,有空可以研究研究。
1. 其实每次调⽤contact()⽅法就是⼀次数组的拷贝,虽然在内存中是处理都是原⼦性操作,速度⾮常快,但是,最后的return语句会创
建⼀个新String对象,限制了concat⽅法的速度。
public String concat(String str){
int otherLen = str.length();
if(otherLen ==0){
return this;
}
int len = value.length;
char buf[]= pyOf(value, len + otherLen);
return new String(buf,true);
}
2. StringBuffer 和 StringBuilder 的append⽅法都继承⾃AbstractStringBuilder,整个逻辑都只做字符数组的加长,拷贝,到最后
也不会创建新的String对象,所以速度很快,完成拼接处理后在程序中⽤String()来得到最终的字符串。
/**
* 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);
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 >0)
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 <0)
newCapacity = minimumCapacity;
if(newCapacity <0){
if(minimumCapacity <0)// overflow
throw new OutOfMemoryError();
newCapacity = Integer.MAX_VALUE;
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论