1.+号拼接
2.concat 拼接
3.join拼接
4.StringBuffer拼接
5.StringBuilder拼接
public class test { public static void main(String[] args) { String add = add(); String concat = concat(); String join = join(); String buffer = buffer(); String builder = builder(); } public static String add() { String string = ""; long startTime = System.currentTimeMillis(); for (int i = 0; i < 1000000; i++) { string += "a"; } long endTime = System.currentTimeMillis(); System.out.println("+号拼接耗时:" + (endTime - startTime)); return string; } public static String concat() { String string = ""; long startTime = System.currentTimeMillis(); for (int i = 0; i < 1000000; i++) { string = string.concat("a"); } long endTime = System.currentTimeMillis(); System.out.println("concat拼接耗时:" + (endTime - startTime)); return string; } public static String join() { String string = ""; long startTime = System.currentTimeMillis(); List<String> list = new ArrayList<String>(); for (int i = 0; i < 1000000; i++) { list.add("a"); } string = String.join(string, list); long endTime = System.currentTimeMillis(); System.out.println("join拼接耗时:" + (endTime - startTime)); return string; } public static String buffer() { StringBuffer sb = new StringBuffer(); long startTime = System.currentTimeMillis(); for (int i = 0; i < 1000000; i++) { sb.append("a"); } long endTime = System.currentTimeMillis(); System.out.println("StringBuffer拼接耗时:" + (endTime - startTime)); return sb.toString(); } public static String builder() { StringBuilder sb = new StringBuilder(); long startTime = System.currentTimeMillis(); for (int i = 0; i < 1000000; i++) { sb.append("a"); } long endTime = System.currentTimeMillis(); System.out.println("StringBuilder拼接耗时:" + (endTime - startTime)); return sb.toString(); } }
执行效率
+号拼接耗时:6501
concat拼接耗时:1739
join拼接耗时:13
StringBuffer拼接耗时:2 (线程安全,多线程推荐)
StringBuilder拼接耗时:1 (线程不安全)