Best way to concatenate List of String objects?

java, list, string

Solution

Your approach is dependent on Java's ArrayList#toString() implementation.

While the implementation is documented in the Java API and very unlikely to change, there's a chance it could. It's far more reliable to implement this yourself (loops, StringBuilders, recursion whatever you like better).

Sure this approach may seem "neater" or more "too sweet" or "money" but it is, in my opinion, a worse approach.

Problem

What is the best way to concatenate a list of String objects? I am thinking of doing this way: ``` List<String> sList = new ArrayList<String>(); // add elements if (sList != null) { String listString = sList.toString(); listString = listString.subString(1, listString.length() - 1); } ``` I somehow found this to be neater than using the StringBuilder/StringBuffer approach. Any thoughts/comments?

Original source

Related problems