ZetCode

Java List to String

Last modified October 13, 2026 Java 25

This tutorial explains different techniques to convert a List into a single string in Java. Java offers several ways to do this, each suited for different scenarios:

The examples use compact source files with an instance main method, final since Java 25. They can be run directly with java Main.java.

Using toString

Every list has a toString method. The implementation used by the standard collections returns the elements in square brackets, separated by a comma and a space. Each element is converted with its own toString method.

Main.java
void main() {

    List<String> fruits = List.of("apple", "banana", "orange");
    List<Integer> numbers = List.of(1, 2, 3, 4);
    List<List<Integer>> nested = List.of(List.of(1, 2), List.of(3, 4));

    System.out.println(fruits.toString());
    System.out.println(numbers);
    System.out.println(nested);
    System.out.println("Fruits: " + fruits);
}

The println method and string concatenation call toString implicitly. The format is fixed, so for any custom output we use one of the methods described below.

$ java Main.java
[apple, banana, orange]
[1, 2, 3, 4]
[[1, 2], [3, 4]]
Fruits: [apple, banana, orange]

Using String.join

The String.join method is the simplest way to join elements with a delimiter. It accepts any Iterable of CharSequence elements, such as a List<String>, or a variable number of arguments.

Main.java
void main() {

    List<String> words = List.of("a", "visit", "to", "London");

    String result = String.join("-", words);
    System.out.println(result);

    String path = String.join("/", "home", "user", "docs");
    System.out.println(path);
}

The first call creates a hyphen-separated string from the list. The second joins its varargs arguments with a slash. Note that String.join does not accept a List<Integer>; non-string elements must be converted first, as shown later.

$ java Main.java
a-visit-to-London
home/user/docs

Using Collectors.joining

The Collectors.joining collector concatenates the elements of a stream of strings. It has three forms: without arguments, with a delimiter, and with a delimiter, prefix, and suffix. Since it works on streams, the elements can be transformed or filtered before they are joined.

Main.java
import java.util.stream.Collectors;

void main() {

    List<String> words = List.of("There", "are", "three", "chairs");

    String sentence = words.stream()
            .collect(Collectors.joining(" "));
    System.out.println(sentence);

    String upper = words.stream()
            .map(String::toUpperCase)
            .collect(Collectors.joining(", ", "{", "}"));
    System.out.println(upper);

    String concatenated = words.stream()
            .collect(Collectors.joining());
    System.out.println(concatenated);
}

The first stream joins the words with a space. The second converts them to uppercase and wraps the result in curly brackets. The third concatenates the words without any delimiter.

$ java Main.java
There are three chairs
{THERE, ARE, THREE, CHAIRS}
Therearethreechairs

Converting lists of non-strings

To join a list of numbers or other objects, we first map each element to a string with the stream map method. The mapping function can also select a field or format the element in any way.

Main.java
import java.util.stream.Collectors;

record User(String name, int age) {}

void main() {

    List<Integer> numbers = List.of(1, 2, 3, 4);

    String csv = numbers.stream()
            .map(String::valueOf)
            .collect(Collectors.joining(","));
    System.out.println(csv);

    List<User> users = List.of(new User("John", 34), new User("Roger", 25),
            new User("Paula", 41));

    String names = users.stream()
            .map(User::name)
            .collect(Collectors.joining(", "));
    System.out.println(names);

    String details = users.stream()
            .map(u -> "%s (%d)".formatted(u.name(), u.age()))
            .collect(Collectors.joining("; "));
    System.out.println(details);
}

We convert a list of integers to a comma-separated string. Then we join the names of users, and finally we format each user with its name and age.

$ java Main.java
1,2,3,4
John, Roger, Paula
John (34); Roger (25); Paula (41)

Using StringJoiner

The StringJoiner class builds a string from elements separated by a delimiter, with an optional prefix and suffix. It is handy when the elements are added one at a time, for instance from several places in the code.

Main.java
void main() {

    List<String> fruits = List.of("apple", "banana", "orange");

    var joiner = new StringJoiner(", ", "[", "]");
    fruits.forEach(joiner::add);
    System.out.println(joiner);

    var empty = new StringJoiner(", ", "[", "]");
    empty.setEmptyValue("no fruits");
    System.out.println(empty);
}

The first joiner separates the elements with a comma and wraps the result in square brackets. The setEmptyValue method sets the text returned when no elements have been added; without it, the result would be [].

$ java Main.java
[apple, banana, orange]
no fruits

Using StringBuilder

StringBuilder gives full control over the concatenation process. It appends to an internal buffer without creating intermediate strings. It is useful when the joining logic is more involved than a simple delimiter. For plain delimited output, String.join is just as efficient and shorter.

Main.java
void main() {

    List<String> words = List.of("There", "are", "three", "chairs");

    var builder = new StringBuilder();

    for (String word : words) {

        if (!builder.isEmpty()) {
            builder.append(' ');
        }

        builder.append(word);
    }

    String result = builder.toString();
    System.out.println(result);
}

A space is appended before every word except the first. This avoids a trailing delimiter. Removing it afterwards with trim would be fragile, because trim also strips whitespace that belongs to the first or last element.

$ java Main.java
There are three chairs

String concatenation in a loop

We can also concatenate the elements with the += operator in a loop. The approach is easy to understand, but it is inefficient for larger lists: strings are immutable, so each iteration creates a new string and copies all the characters collected so far.

Main.java
void main() {

    List<String> words = List.of("one", "two", "three");
    String result = "";

    for (int i = 0; i < words.size(); i++) {

        result += words.get(i);

        if (i < words.size() - 1) {
            result += ", ";
        }
    }

    System.out.println(result);
}

The example concatenates the elements of a list into a comma-separated string. The delimiter is not added after the last element.

$ java Main.java
one, two, three

Handling null elements

Lists such as ArrayList or the list returned by Arrays.asList may contain null elements, while List.of rejects them. toString, String.join, and Collectors.joining turn a null element into the text "null". If that is not wanted, we can filter the null values out or replace them.

Main.java
import java.util.stream.Collectors;

void main() {

    List<String> words = Arrays.asList("sky", null, "blue");

    System.out.println(words);
    System.out.println(String.join(", ", words));

    String filtered = words.stream()
            .filter(Objects::nonNull)
            .collect(Collectors.joining(", "));
    System.out.println(filtered);

    String replaced = words.stream()
            .map(w -> Objects.requireNonNullElse(w, "?"))
            .collect(Collectors.joining(", "));
    System.out.println(replaced);
}

The filter call drops the null elements, and Objects.requireNonNullElse replaces them with a question mark.

$ java Main.java
[sky, null, blue]
sky, null, blue
sky, blue
sky, ?, blue

Which method to use

Avoid += concatenation in loops over large lists.

Source

Java String.join documentation

Java Collectors documentation

This tutorial has demonstrated various methods to convert a List to a string in Java.

Author

My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.

List all Java tutorials.