Table of contents
1.
Introduction
2.
Example of List-to-String Conversion
2.1.
Example
3.
Methods to Convert List to String in Java
3.1.
1. Using StringBuilder class
3.2.
2. Using join() method of String class
3.3.
3. Using List.toString(), String.substring(), and String.replaceAll() methods
3.4.
4. Using Collectors.joining() from Stream API
4.
Frequently Asked Questions
4.1.
What is the most efficient way to convert a list to a string in Java? 
4.2.
Can I convert a list of objects to a string using these methods? 
4.3.
What is the easiest method to convert a list to a string in Java 8 and later? 
5.
Conclusion
Last Updated: Mar 4, 2025
Easy

Converting a List to String in Java

Author Pallavi singh
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In Java, converting a List to a String is a common operation when handling collections of data. This can be done using methods like String.join(), Stream API, or StringBuilder for efficient concatenation. The conversion is useful for displaying list elements in a readable format or passing data as a single string.

Converting a List to String in Java

In this article, you will learn different ways to convert a List to a String in Java, along with examples and best practices.

Example of List-to-String Conversion

Before diving into different methods, let's take a simple example where we have a list of strings and need to convert it into a single string.

Example

import java.util.*;
public class ListToStringExample {
    public static void main(String[] args) {
        List<String> fruits = Arrays.asList("Apple", "Banana", "Cherry", "Mango");
        String result = String.join(", ", fruits);
        System.out.println("Converted String: " + result);
    }
}
You can also try this code with Online Java Compiler
Run Code

 

Output:

Converted String: Apple, Banana, Cherry, Mango


This is just one way to convert a list to a string. Let's discuss different methods in detail.

Methods to Convert List to String in Java

Java offers several methods to perform this conversion. Below are some of the most commonly used approaches:

1. Using StringBuilder class

StringBuilder is an efficient way to convert a list to a string because it avoids unnecessary string object creation.

import java.util.*;

public class StringBuilderExample {
    public static void main(String[] args) {
        List<String> colors = Arrays.asList("Red", "Green", "Blue");
        StringBuilder sb = new StringBuilder();
        
        for (String color : colors) {
            sb.append(color).append(", ");
        }
        
        String result = sb.substring(0, sb.length() - 2); // Removing the last comma and space
        System.out.println("Converted String: " + result);
    }
}
You can also try this code with Online Java Compiler
Run Code

 

Output

Converted String: Red, Green, Blue

 

This method is efficient as it minimizes the creation of multiple string objects.

2. Using join() method of String class

Java 8 introduced the join() method in the String class, which makes list-to-string conversion simple.

import java.util.*;
public class JoinExample {
    public static void main(String[] args) {
        Lhttps://www.naukri.com/code360/library/string-class-methods, "Doe");
        String result = String.join(" - ", names);
        System.out.println("Converted String: " + result);
    }
}
You can also try this code with Online Java Compiler
Run Code

 

Output:

Converted String: John - Jane - Doe

 

This method is concise and easy to implement in Java 8 and later versions.

3. Using List.toString(), String.substring(), and String.replaceAll() methods

The toString() method returns a string representation of a list, but it includes square brackets. We can use substring() and replaceAll() to format it properly.

import java.util.*;
public class ToStringExample {
    public static void main(String[] args) {
        List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
        String result = numbers.toString();
        result = result.substring(1, result.length() - 1); // Removing square brackets
        System.out.println("Converted String: " + result);
    }
}
You can also try this code with Online Java Compiler
Run Code

 

Output:

Converted String: 1, 2, 3, 4, 5

 

This approach is quick but not the most efficient, as it involves string manipulation.

4. Using Collectors.joining() from Stream API

The Collectors.joining() method is a part of the Stream API in Java 8 and provides a straightforward way to convert a list to a string.

import java.util.*;
import java.util.stream.Collectors;
public class CollectorsExample {
    public static void main(String[] args) {
        List<String> animals = Arrays.asList("Dog", "Cat", "Elephant");
        String result = animals.stream().collect(Collectors.joining(", "));
        System.out.println("Converted String: " + result);
    }
}
You can also try this code with Online Java Compiler
Run Code

 

Output:

Converted String: Dog, Cat, Elephant

 

This method is highly recommended when working with Java Streams, as it is clean and efficient.

Frequently Asked Questions

What is the most efficient way to convert a list to a string in Java? 

The most efficient way is using StringBuilder, as it avoids unnecessary string object creation.

Can I convert a list of objects to a string using these methods? 

Yes, but you need to override the toString() method of the object class or use a lambda expression in Java Streams.

What is the easiest method to convert a list to a string in Java 8 and later? 

Using Collectors.joining() from the Stream API is the easiest and most readable way to convert a list to a string.

Conclusion

In this article, we discussed different methods to convert a list to a string in Java. We covered methods like StringBuilder for efficient conversion, join() method of String class, toString() with string manipulation and Using the Collectors.joining() method from Stream  API. Each method has its advantages, and the choice depends on the specific use case. If performance is critical, StringBuilder is a great choice. For simplicity, join() or Collectors.joining() can be used.

Live masterclass