Explain Codes LogoExplain Codes Logo

Best way to concatenate List of String objects?

java
string-concatenation
stringbuilder
java-streams-api
Alex KataevbyAlex Kataev·Jan 21, 2025
TLDR

For a quick, efficient concatenation of a List<String>, you can use:

Java's built-in method String.join:

// Adds all values in the list with nothing between them - not even space! String result = String.join("", list);

Alternatively, leverage Java 8 Stream API for more flexibility:

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

Both methods are concise and gracefully handle null values. String.join provides simplicity, while streams grant more control.

Keeping things separated: using delimiters

If you want to keep things a little more organized, both String.join and StringUtils.join can accommodate delimiters.

Using String.join with a delimiter:

// Adding a comma and space, because even Strings need personal space. String result = String.join(", ", list);

Using StringUtils.join from Apache Commons:

String result = StringUtils.join(list, ", ");

These methods help optimize efficiency, especially for large lists, preventing any manual looping nightmares.

DIY concatenation: StringBuilder

Sure, library methods are great, but what if you want to do it yourself? Enter, StringBuilder. It offers robustness and flexibility, particularly if you have specific needs that cannot be fulfilled by the library methods:

StringBuilder sb = new StringBuilder(); for (String str : list) { // They laughed when I said I could build a Builder. Who’s laughing now? sb.append(str); } String result = sb.toString();

Android has your back: TextUtils.join

Different environments often offer unique handy tools. For instance in Android SDK, TextUtils.join provides an efficient tool customized for Android development:

String result = TextUtils.join(", ", list);

The power user's tool: Advanced concatenation with Java Streams API

Handling more intricate scenarios often necessitates a more powerful tool. Enter, Java Streams API:

String result = list.stream().map(Object::toString) .collect(Collectors.joining(", ", "[", "]"));

This elegant piece of code concatenates strings, inserts a comma as a delimiter, and wraps the result into square brackets. Now, that’s what I call power!

Don't fall into the trap: Avoiding pitfalls

Watch out for ArrayList.toString(). It might seem tempting, but remember, it’s dependent on Java’s specific implementation, and who knows what might happen in the future?

// Because relying on features that can change is like building a house on sand! String result = list.toString();

Stick with our tried and tested methods to avoid any future unpleasant surprises.

Future-proof your Code: Make it Robust

Creating a custom method shields your code from any future Java implementation changes:

public static String customJoin(List<String> items, String delimiter) { return items.stream().collect(Collectors.joining(delimiter)); }

This could be your lifeboat in a sea of constant Java updates. Here, you have the helm of control - steer it as you please!