Bonjour,

Je cherche à trouver la manière la plus simple, rapide et élégante de concacter une list<String> en String avec chaque valeur séparées par un séparateur.

Voici les 3 solutions que j'ai relevé :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
public static String firstConcat(List<String> list, String sep) {
        StringBuilder resultBuilder = new StringBuilder();
        boolean firstValue = true;
        for (String value : list) {
            if (firstValue) {
                firstValue = false;
            } else {
                resultBuilder.append(sep);
            }
            resultBuilder.append(value);
        }
        return resultBuilder.toString();
    }
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
public static String secondConcat(List<String> list, String sep) {
        StringBuilder resultBuilder = new StringBuilder();
        for (String value : list) {
            resultBuilder.append(value);
            resultBuilder.append(sep);
        }
        if (resultBuilder.length() == 0) {
            return "";
        } else {
            return resultBuilder.substring(0, resultBuilder.length() - 1);
        }
    }
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
public static String thirdConcat(List<String> list, String sep) {
        StringBuilder resultBuilder = new StringBuilder();
        String mySep = "";
        for (String value : list) {
            resultBuilder.append(mySep);
            resultBuilder.append(value);
            mySep = sep;
        }
        return resultBuilder.toString();
    }
Un test de performance ne permet pas de privilégié une solution plus qu'une autre, les timers change tout le temps
J'aimerais avoir votre avis sur la meilleur solution, ou votre propre solution si vous en avez une.

Merci

Code de test :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package sandbox;
 
import java.util.LinkedList;
import java.util.List;
 
public class Concat {
 
    public static void main(String[] args) {
        for (int i = 0; i < 7; i++) {
            test(i*1000000);
        }
    }
 
    public static void test(int nbValues) {
        List<String> list = new LinkedList<>();
        for (int i = 0; i < nbValues; i++) {
            list.add(String.valueOf(i));
        }
 
        System.out.println("nbValues : " + nbValues);
        long time;
 
        time = System.currentTimeMillis();
        firstConcat(list, ",");
        long timerFirstConcat = System.currentTimeMillis() - time;
 
        time = System.currentTimeMillis();
        secondConcat(list, ",");
        long timerSecondConcat = System.currentTimeMillis() - time;
 
        time = System.currentTimeMillis();
        thirdConcat(list, ",");
        long timerThirdConcat = System.currentTimeMillis() - time;
 
        System.out.println("first : " + timerFirstConcat+" | second : " + timerSecondConcat+" | third :  "+ timerThirdConcat);
    }
 
    public static String firstConcat(List<String> list, String sep) {
        StringBuilder resultBuilder = new StringBuilder();
        boolean firstValue = true;
        for (String value : list) {
            if (firstValue) {
                firstValue = false;
            } else {
                resultBuilder.append(sep);
            }
            resultBuilder.append(value);
        }
        return resultBuilder.toString();
    }
 
    public static String secondConcat(List<String> list, String sep) {
        StringBuilder resultBuilder = new StringBuilder();
        for (String value : list) {
            resultBuilder.append(value);
            resultBuilder.append(sep);
        }
        if (resultBuilder.length() == 0) {
            return "";
        } else {
            return resultBuilder.substring(0, resultBuilder.length() - 1);
        }
    }
 
    public static String thirdConcat(List<String> list, String sep) {
        StringBuilder resultBuilder = new StringBuilder();
        String mySep = "";
        for (String value : list) {
            resultBuilder.append(mySep);
            resultBuilder.append(value);
            mySep = sep;
        }
        return resultBuilder.toString();
    }
 
}