List to Comma Separated String in Java
- String.join(", ", list) is the simplest approach for a List<String>. Collectors.joining() is more flexible and integrates into stream pipelines.
- For List<Integer> or other non-String lists, stream with .map(String::valueOf) before joining.
- Collectors.joining(delimiter, prefix, suffix) adds wrapping characters β useful for building JSON arrays, SQL IN clauses, or bracketed lists.
List to CSV string is one of those operations that looks trivial and has a surprising number of correct answers depending on your requirements. String.join() for the simple case. Collectors.joining() for stream pipelines. StringBuilder for complex transformation requirements or pre-Java 8 codebases.
String.join(), Collectors.joining(), and StringBuilder
Three approaches cover all real-world cases. The Java 8+ stream API version (Collectors.joining()) is the most flexible and composes well with filtering and transforming the list before joining.
package io.thecodeforge.strings; import java.util.List; import java.util.stream.Collectors; public class ListToCommaSeparatedString { public static void main(String[] args) { List<String> products = List.of("PaymentService", "OrderService", "AuditService"); // Method 1: String.join() β simplest, Java 8+ String joined = String.join(", ", products); System.out.println(joined); // PaymentService, OrderService, AuditService // Method 2: Collectors.joining() β stream pipeline, full control String withBrackets = products.stream() .collect(Collectors.joining(", ", "[", "]")); System.out.println(withBrackets); // [PaymentService, OrderService, AuditService] // Combine with filter/transform in the stream List<String> services = List.of("PaymentService", null, "AuditService", ""); String cleaned = services.stream() .filter(s -> s != null && !s.isBlank()) .collect(Collectors.joining(", ")); System.out.println(cleaned); // PaymentService, AuditService // Method 3: Join a list of integers (must convert to String) List<Integer> ids = List.of(101, 102, 103, 104); String idList = ids.stream() .map(String::valueOf) .collect(Collectors.joining(", ")); System.out.println(idList); // 101, 102, 103, 104 // SQL IN clause builder String inClause = "SELECT * FROM orders WHERE id IN (" + ids.stream().map(String::valueOf).collect(Collectors.joining(", ")) + ")"; System.out.println(inClause); } }
[PaymentService, OrderService, AuditService]
PaymentService, AuditService
101, 102, 103, 104
SELECT * FROM orders WHERE id IN (101, 102, 103, 104)
| Method | Java Version | Handles Null? | Custom Prefix/Suffix? | Best For |
|---|---|---|---|---|
| String.join() | Java 8+ | Writes 'null' | No | Simple List<String> join |
| Collectors.joining() | Java 8+ | NPE on null | Yes | Stream pipelines with filter/transform |
| StringJoiner | Java 8+ | Configurable | Yes | Programmatic building |
| StringBuilder loop | All versions | Full control | Yes | Pre-Java 8 or complex logic |
π― Key Takeaways
- String.join(", ", list) is the simplest approach for a List<String>. Collectors.joining() is more flexible and integrates into stream pipelines.
- For List<Integer> or other non-String lists, stream with .map(String::valueOf) before joining.
- Collectors.joining(delimiter, prefix, suffix) adds wrapping characters β useful for building JSON arrays, SQL IN clauses, or bracketed lists.
- Filter nulls and blanks before joining: stream().filter(s -> s != null && !s.isBlank()).collect(Collectors.joining(', ')).
β Common Mistakes to Avoid
- βUsing String.join() on a List containing nulls β null elements are converted to the literal string 'null'. Filter them out first with stream().filter(Objects::nonNull).
- βBuilding SQL IN clauses with string concatenation from user input β if the list comes from user input, use parameterised queries, not string joining, to prevent SQL injection.
Interview Questions on This Topic
- QHow would you convert a List<Integer> to a comma-separated String in Java 8+?
- QWhat is the difference between String.join() and Collectors.joining()?
Frequently Asked Questions
How do I convert a List to a comma separated String in Java?
Use String.join(", ", list) for a simple List<String>. For more control or non-String lists, use stream().collect(Collectors.joining(", ")). For non-String types, add a .map(String::valueOf) before the collect.
How do I join a list with a prefix and suffix?
Use Collectors.joining(delimiter, prefix, suffix): list.stream().collect(Collectors.joining(", ", "[", "]")) produces [item1, item2, item3].
Developer and founder of TheCodeForge. I built this site because I was tired of tutorials that explain what to type without explaining why it works. Every article here is written to make concepts actually click.