ZetCode

Java List to String

Last modified May 31, 2025

This tutorial explains different techniques to convert a List into a single string in Java.

Java offers several efficient methods for joining list elements into a string, each suited for different scenarios:

By choosing the right method, you can optimize performance and readability based on your specific use case.

Using String.join

The simplest way to join list elements with a delimiter:

Main.java
void main() {

    List<String> words = List.of("a", "visit", "to", "London");
    String result = String.join("-", words);
    
    System.out.println(result);
}

This creates a hyphen-separated string from the list. Works with any List<String>.

Using Stream and Collectors.joining

The Collectors.joining method is useful for joining elements in a stream into a single string with a specified delimiter.

Main.java
void main() {

    List<String> words = List.of("There", "are", "three", "chairs");
    String result = words.stream().collect(Collectors.joining(" "));
    
    System.out.println(result);
}

This joins the list elements with a space. You can use any delimiter or even no delimiter by passing an empty string.

Using StringBuilder

When you need more control over the concatenation process, such as handling complex logic or large lists, StringBuilder is a great choice. It allows you to append elements efficiently without creating multiple intermediate strings, which is important for performance.

Main.java
void main() {

    List<String> words = List.of("There", "are", "three", "chairs");
    StringBuilder builder = new StringBuilder();
    
    for (String word : words) {
        builder.append(word).append(" ");
    }
    
    String result = builder.toString().trim();
    System.out.println(result);
}

StringBuilder is ideal for large lists or complex concatenation logic.

Using StringJoiner

The StringJoiner class is useful when you need to create a string with a specific prefix, suffix, and delimiter. It provides a clean way to build strings with these features.

Main.java
void main() {

    List<String> words = List.of("apple", "banana", "orange");
    StringJoiner joiner = new StringJoiner(", ", "[", "]");
    
    words.forEach(joiner::add);
    
    System.out.println(joiner.toString());
}

In the example above, we create a StringJoiner that joins the elements with a comma, and wraps the result in square brackets.

Manual String Concatenation in a Loop

You can manually concatenate list elements into a string using a simple loop. While this approach is less efficient for large lists compared to using StringBuilder or String.join.

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);
}

This example concatenates the elements of a list into a comma-separated string using a manual loop.

Converting List of Non-Strings

If you have a list of non-string elements (like integers), you can convert them to strings using the map function in a stream before joining them. This is particularly useful when you want to format a list of numbers or other types into a single string.

Main.java
void main() {

    List<Integer> numbers = List.of(1, 2, 3, 4);
    
    String result = numbers.stream()
                         .map(String::valueOf)
                         .collect(Collectors.joining(","));
    
    System.out.println(result);
}

This converts a list of integers into a comma-separated string.

Source

Java String.join 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.