Java 拼接字符串的幾種寫法

用某個分隔符拼接字符串

寫程序時常常遇到須要將集合的元素拼接成字符串,而後存入數據庫的狀況。
下邊是我使用過的幾種方式java

  1. 使用流
  2. StringBuilder
  3. StringJoiner
  4. Guava

代碼

初始數據

List<String> list = Arrays.asList("Luffy", "Zoro", "Nami", 
                                  "Usopp", "Sanji", "Chopper", 
                                  "Robin", "FRANKY", "BROOK");

1. 使用流

String result = list.stream().collect(Collectors.joining(","));

2. StringBuilder

StringBuilder strBur = new StringBuilder();
list.forEach(val -> {
    strBur.append(val).append(",");
});
strBur.toString();

3. StringJoiner

StringJoiner strJor = new StringJoiner(",");
for(String str : list) {
    strJor.add(str);
}
strJor.toString();

4. Guava

String result = Joiner.on(",").join(list);

性能測試

package com.cck;                                                                                                                                                                                                                                                                                                      
import java.util.Arrays;                                                                                                                                
import java.util.List;                                                                                                                                  
import java.util.StringJoiner;                                                                                                                          
import java.util.function.Consumer;                                                                                                                     
import java.util.stream.Collectors;                                                                                                                     
                                                                                                                                                        
import com.google.common.base.Joiner;                                                                                                                   
                                                                                                                                                        
public class Test {                                                                                                                                     
                                                                                                                                                        
                                                                                                                                                        
    // 1.使用流                                                                                                                                            
    public static Consumer<List<String>> testStream() {                                                                                                 
                                                                                                                                                        
        return (list) -> {                                                                                                                              
                                                                                                                                                        
            String result = list.stream().collect(Collectors.joining(","));                                                                             
        };                                                                                                                                              
    }                                                                                                                                                   
                                                                                                                                                        
    // 2.StringBuilder                                                                                                                                  
    public static Consumer<List<String>> testStringBuilder() {                                                                                          
                                                                                                                                                        
        return (list) -> {                                                                                                                              
                                                                                                                                                        
            StringBuilder strBur = new StringBuilder();                                                                                                 
            list.forEach(val -> {                                                                                                                       
                strBur.append(val).append(",");                                                                                                         
            });                                                                                                                                         
            strBur.toString();                                                                                                                          
        };                                                                                                                                              
    }                                                                                                                                                   
                                                                                                                                                        
    // 3.StringJoiner                                                                                                                                   
    public static Consumer<List<String>> testStringJoiner() {                                                                                           
                                                                                                                                                        
        return (list) -> {                                                                                                                              
                                                                                                                                                        
            StringJoiner strjor = new StringJoiner(",");                                                                                                
            for(String str : list) {                                                                                                                    
                strjor.add(str);                                                                                                                        
            }                                                                                                                                           
            strjor.toString();                                                                                                                          
        };                                                                                                                                              
    }                                                                                                                                                   
                                                                                                                                                        
    // 4.Guava                                                                                                                                          
    public static Consumer<List<String>> testGuava() {                                                                                                  
                                                                                                                                                        
        return (list) -> {                                                                                                                              
                                                                                                                                                        
            String result = Joiner.on(",").join(list);                                                                                                  
        };                                                                                                                                              
    }                                                                                                                                                   
                                                                                                                                                        
    public static void test(Consumer<List<String>> consumer, 
                                List<String> list, String msg) {                                                           
                                                                                                                                                        
        long start = System.currentTimeMillis();                                                                                                        
        for (int i = 0; i < 1000000; i++) {                                                                                                             
            consumer.accept(list);                                                                                                                      
        }                                                                                                                                               
        long end = System.currentTimeMillis();                                                                                                          
                                                                                                                                                        
        System.out.println(msg + " need time : " + (end - start));                                                                                      
    }                                                                                                                                                   
                                                                                                                                                        
    public static void main(String[] args) {                                                                                                            
                                                                                                                                                        
        List<String> list = Arrays.asList("Luffy", "Zoro", "Nami", 
                                          "Usopp", "Sanji", "Chopper", 
                                          "Robin", "FRANKY", "BROOK");   
                                                                   
        test(testStream(),        list, "1.使用流");                                                                                                              
        test(testStringBuilder(), list, "2.StringBuilder");                                                                                             
        test(testStringJoiner(),  list, "3.StringJoiner");                                                                                               
        test(testGuava(),         list, "4.Guava");                                                                                                             
    }                                                                                                                                                   
                                                                                                                                                        
}
執行100w次用時以下:
100w

執行10w次用時以下:
圖片描述數據庫

感受須要根據運算量去決定用哪一種方法。
在沒特別要求的狀況下,本身仍是使用Guava的多一些,由於看起來好看些。app

相關文章
相關標籤/搜索