Java
String Conversion
Programming
Data Structures
Code Optimization

Java convert List<String> to a join()d String

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

Introduction

Joining a List<String> into one string is a small task that appears everywhere: logging, CSV-like output, SQL fragments, UI labels, and HTTP headers. Java has several good ways to do it, and the best choice depends on whether you need plain joining, stream-based transformations, or compatibility with older Java versions. The important part is to pick a method that matches the surrounding code instead of building manual loops by habit.

Use String.join for the Simple Case

If you already have a List<String> and only need a delimiter, String.join is the clearest option.

java
1import java.util.List;
2
3public class JoinExample {
4    public static void main(String[] args) {
5        List<String> fruits = List.of("apple", "banana", "cherry");
6        String joined = String.join(", ", fruits);
7        System.out.println(joined);
8    }
9}

This prints apple, banana, cherry. The method is concise, readable, and ideal when the input is already a collection of strings.

Use Collectors.joining When Transformation Is Needed

If the input needs mapping or filtering before joining, the stream API is usually a better fit. Collectors.joining works well when joining is only the final step of a longer pipeline.

java
1import java.util.List;
2import java.util.stream.Collectors;
3
4public class StreamJoinExample {
5    public static void main(String[] args) {
6        List<String> values = List.of("  red  ", "blue", "", "green");
7
8        String joined = values.stream()
9                .map(String::trim)
10                .filter(s -> !s.isEmpty())
11                .collect(Collectors.joining(" | "));
12
13        System.out.println(joined);
14    }
15}

This approach is more flexible than String.join because you can normalize the values before combining them.

Collectors.joining also supports prefix and suffix values.

java
String result = List.of("A", "B", "C").stream()
        .collect(Collectors.joining(", ", "[", "]"));
System.out.println(result);

That prints [A, B, C].

Manual Joining for Older Code or Special Control

If you are working on older Java versions or you need very custom formatting logic, a StringBuilder loop is still valid.

java
1import java.util.Arrays;
2import java.util.List;
3
4public class BuilderJoinExample {
5    public static void main(String[] args) {
6        List<String> items = Arrays.asList("one", "two", "three");
7        StringBuilder builder = new StringBuilder();
8
9        for (int i = 0; i < items.size(); i++) {
10            if (i > 0) {
11                builder.append(", ");
12            }
13            builder.append(items.get(i));
14        }
15
16        System.out.println(builder);
17    }
18}

This is more verbose, but it makes the delimiter logic explicit and works without streams.

Be Careful With null Values

A frequent source of bugs is forgetting that some lists contain null. Joining methods do not automatically fix bad input. The safest answer is usually to decide on a policy first:

  • remove null values
  • replace them with a placeholder
  • fail fast

Here is a stream-based example that filters them out.

java
1import java.util.Arrays;
2import java.util.List;
3import java.util.Objects;
4import java.util.stream.Collectors;
5
6public class NullSafeJoinExample {
7    public static void main(String[] args) {
8        List<String> items = Arrays.asList("alpha", null, "gamma");
9
10        String joined = items.stream()
11                .filter(Objects::nonNull)
12                .collect(Collectors.joining(", "));
13
14        System.out.println(joined);
15    }
16}

If null has business meaning, replacing it with a label such as missing may be better than filtering it out silently.

Choosing the Right Option

Use String.join when the code already has strings and only needs a delimiter. Use Collectors.joining when the values need transformation, filtering, or additional formatting in the same pipeline. Use StringBuilder when you need compatibility with older codebases or you want fully custom iteration logic.

Performance differences are usually small compared with the cost of unclear code. Unless profiling shows otherwise, choose the most readable option for the context.

Common Pitfalls

One common mistake is using streams for a trivial join where String.join would be simpler. The extra pipeline adds noise without adding value.

Another issue is ignoring null values and assuming the join operation will handle them in a useful way. Decide explicitly how null should be treated.

Developers also sometimes build strings with repeated + concatenation inside loops. That creates unnecessary intermediate strings and is harder to read than the built-in joining options.

Finally, remember that joining is formatting, not serialization. If the output must be a valid CSV or JSON representation, use a format-aware library instead of a plain delimiter join.

Summary

  • Use String.join for straightforward joining of an existing List<String>.
  • Use Collectors.joining when you need mapping, filtering, prefix, or suffix behavior.
  • Use StringBuilder for older Java code or specialized formatting logic.
  • Handle null values deliberately instead of assuming a default behavior.
  • Prefer the clearest option unless measurement proves a real performance issue.

Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

All Rights Reserved.