Java Tricks: Fastest Way to Collecting Objects in a String

The fastest way to collect a list of objects in a String in Java:

StringBuilder buffer = new StringBuilder ();
String delim = "";
for (Object o: list)
{
    buffer.append (delim);
    delim = ", "; // Avoid if(); assignment is very fast!
    buffer.append (o);
}
buffer.toString ();

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.