Homeβ€Ί Javaβ€Ί List to Comma Separated String in Java

List to Comma Separated String in Java

Where developers are forged. Β· Structured learning Β· Free forever.
πŸ“ Part of: Strings β†’ Topic 12 of 15
Convert a Java List to a comma separated string using String.
πŸ§‘β€πŸ’» Beginner-friendly β€” no prior Java experience needed
In this tutorial, you'll learn:
  • 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.
✦ Plain-English analogy ✦ Real code with output ✦ Interview questions
⚑ Quick Answer
You have a list of values and need them as a single string with commas between them β€” for a CSV, a log message, an SQL IN clause, or an API parameter. Java gives you several ways and the right choice depends on how much control you need over the delimiter, prefix, and suffix.

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.

ListToCommaSeparatedString.java Β· JAVA
123456789101112131415161718192021222324252627282930313233343536373839
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);
    }
}
β–Ά Output
PaymentService, OrderService, AuditService
[PaymentService, OrderService, AuditService]
PaymentService, AuditService
101, 102, 103, 104
SELECT * FROM orders WHERE id IN (101, 102, 103, 104)
MethodJava VersionHandles Null?Custom Prefix/Suffix?Best For
String.join()Java 8+Writes 'null'NoSimple List<String> join
Collectors.joining()Java 8+NPE on nullYesStream pipelines with filter/transform
StringJoinerJava 8+ConfigurableYesProgrammatic building
StringBuilder loopAll versionsFull controlYesPre-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].

πŸ”₯
Naren Founder & Author

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.

← PreviousChar Array to String in Java: Four Conversion MethodsNext β†’Java String contains(): Check for Substrings
Forged with πŸ”₯ at TheCodeForge.io β€” Where Developers Are Forged