Java 8 – Convert List of Strings to a Single String

Introduction

In Java 8, converting a list of strings into a single concatenated string is a common task. The Stream API simplifies this process by allowing you to use the Collectors.joining() method. This method not only allows you to concatenate strings but also provides options for specifying delimiters, prefixes, and suffixes.

In this guide, we will learn how to convert a list of strings into a single string using Java 8.

Solution Steps

  1. Define the List of Strings: Create a list of strings to be concatenated.
  2. Convert the List to a Stream: Use stream() to convert the list into a stream.
  3. Concatenate Using Collectors.joining(): Use the Collectors.joining() method to concatenate the strings, with or without a delimiter.
  4. Display the Result: Print the resulting concatenated string.

Java Program

Example 1: Concatenating Without a Delimiter

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class ListToSingleStringExample {
    public static void main(String[] args) {
        // Step 1: Define the List of Strings
        List<String> words = Arrays.asList("Java", "is", "awesome");

        // Step 2: Convert the List to a Stream and concatenate without a delimiter
        String result = words.stream()
                             .collect(Collectors.joining());

        // Step 3: Display the Result
        System.out.println(result);
    }
}

Output

Javaisawesome

Explanation

Step 1: Define the List of Strings

We begin by defining a list of strings:

List<String> words = Arrays.asList("Java", "is", "awesome");

This list contains three elements: "Java", "is", and "awesome".

Step 2: Convert the List to a Stream and Concatenate Without a Delimiter

We use the stream() method to convert the list into a stream. Then, we use Collectors.joining() without a delimiter to concatenate the elements:

String result = words.stream()
                     .collect(Collectors.joining());

This produces the string "Javaisawesome".

Step 3: Display the Result

The concatenated string is printed:

System.out.println(result);

Example 2: Concatenating With a Delimiter

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class ListToStringWithDelimiter {
    public static void main(String[] args) {
        // Step 1: Define the List of Strings
        List<String> words = Arrays.asList("Java", "is", "awesome");

        // Step 2: Convert the List to a Stream and concatenate with a space delimiter
        String result = words.stream()
                             .collect(Collectors.joining(" "));

        // Step 3: Display the Result
        System.out.println(result);
    }
}

Output

Java is awesome

Explanation

Step 1: Define the List of Strings

We define the same list of strings:

List<String> words = Arrays.asList("Java", "is", "awesome");

Step 2: Convert the List to a Stream and Concatenate With a Delimiter

We use Collectors.joining(" ") to concatenate the strings with a space as the delimiter:

String result = words.stream()
                     .collect(Collectors.joining(" "));

This results in "Java is awesome".

Step 3: Display the Result

We print the concatenated string:

System.out.println(result);

Example 3: Concatenating With a Delimiter, Prefix, and Suffix

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class ListToStringWithDelimiterPrefixSuffix {
    public static void main(String[] args) {
        // Step 1: Define the List of Strings
        List<String> words = Arrays.asList("Java", "is", "awesome");

        // Step 2: Convert the List to a Stream and concatenate with delimiter, prefix, and suffix
        String result = words.stream()
                             .collect(Collectors.joining(", ", "[", "]"));

        // Step 3: Display the Result
        System.out.println(result);
    }
}

Output

[Java, is, awesome]

Explanation

Step 1: Define the List of Strings

We define the same list of strings:

List<String> words = Arrays.asList("Java", "is", "awesome");

Step 2: Convert the List to a Stream and Concatenate With Delimiter, Prefix, and Suffix

We use Collectors.joining(", ", "[", "]") to concatenate the strings, adding a comma and space as the delimiter, and enclosing the result in square brackets:

String result = words.stream()
                     .collect(Collectors.joining(", ", "[", "]"));

This produces the string "[Java, is, awesome]".

Step 3: Display the Result

The final result is printed:

System.out.println(result);

Conclusion

In Java 8, converting a list of strings into a single string is easy with the Stream API and Collectors.joining(). You can concatenate strings with or without a delimiter, and even add a prefix and suffix to the result. This method is efficient and provides flexibility for various string manipulation tasks.

Comments