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.
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.
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.
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.
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.
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
nullvalues - replace them with a placeholder
- fail fast
Here is a stream-based example that filters them out.
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.joinfor straightforward joining of an existingList<String>. - Use
Collectors.joiningwhen you need mapping, filtering, prefix, or suffix behavior. - Use
StringBuilderfor older Java code or specialized formatting logic. - Handle
nullvalues deliberately instead of assuming a default behavior. - Prefer the clearest option unless measurement proves a real performance issue.
Related reading
- Java, find intersection of two arrays
- Java function for arrays like PHP's join()?
- Java Get first item from a collection
- Java Hashmap How to get key from value?
- Java difference between strong/soft/weak/phantom reference
- Java G1 Old generation garbage collection count is 0
- Java current machine name and logged in user?
- Java Date from unix timestamp

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 courseTrack 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.