Java 8 - Convert Stream of Characters to String

Introduction

In Java 8, converting a Stream<Character> back into a String can be achieved using the Stream API. This is particularly useful when you've performed operations on individual characters in a stream and need to reassemble them into a single string. Java provides an efficient way to do this using Collectors.joining() or StringBuilder.

In this guide, we will learn how to convert a stream of characters into a string using Java 8.

Solution Steps

  1. Create a Stream of Characters: Generate or obtain a Stream<Character> that you want to convert back to a string.
  2. Convert the Stream to a String: Use Collectors.joining() or StringBuilder to concatenate the characters back into a string.
  3. Display the Result: Print or use the resulting string.

Java Program

Method 1: Using Collectors.joining()

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

public class StreamOfCharactersToString {
    public static void main(String[] args) {
        // Step 1: Define a List of Characters
        List<Character> charList = Arrays.asList('H', 'e', 'l', 'l', 'o', ',', ' ', 'W', 'o', 'r', 'l', 'd', '!');

        // Step 2: Convert the Stream of Characters to a String
        String result = charList.stream()
                                .map(String::valueOf)  // Convert each Character to String
                                .collect(Collectors.joining());  // Join them together

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

Output

Hello, World!

Explanation

Step 1: Create a Stream of Characters

We first define a list of characters:

List<Character> charList = Arrays.asList('H', 'e', 'l', 'l', 'o', ',', ' ', 'W', 'o', 'r', 'l', 'd', '!');

Step 2: Convert the Stream to a String

We use map(String::valueOf) to convert each Character into a String, and then Collectors.joining() to concatenate the characters:

String result = charList.stream()
                        .map(String::valueOf)  // Convert each character to String
                        .collect(Collectors.joining());  // Join all together

Step 3: Display the Result

Finally, the resulting string is printed:

System.out.println(result);

Method 2: Using StringBuilder

import java.util.Arrays;
import java.util.List;

public class StreamOfCharactersToStringUsingStringBuilder {
    public static void main(String[] args) {
        // Step 1: Define a List of Characters
        List<Character> charList = Arrays.asList('H', 'e', 'l', 'l', 'o', ',', ' ', 'W', 'o', 'r', 'l', 'd', '!');

        // Step 2: Use StringBuilder to collect the characters into a String
        StringBuilder result = new StringBuilder();
        charList.stream().forEach(result::append);  // Append each character to StringBuilder

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

Output

Hello, World!

Explanation

Step 1: Create a Stream of Characters

We define the list of characters, similar to Method 1:

List<Character> charList = Arrays.asList('H', 'e', 'l', 'l', 'o', ',', ' ', 'W', 'o', 'r', 'l', 'd', '!');

Step 2: Use StringBuilder to Collect Characters

Instead of using Collectors.joining(), we use StringBuilder to efficiently append each character from the stream:

StringBuilder result = new StringBuilder();
charList.stream().forEach(result::append);  // Append characters to StringBuilder

Step 3: Display the Result

The resulting string is printed using toString() from StringBuilder:

System.out.println(result.toString());

Conclusion

Converting a Stream<Character> to a String in Java 8 can be easily done using either Collectors.joining() or StringBuilder. Both approaches are effective, with Collectors.joining() being more concise and StringBuilder being more efficient for large collections of characters. Both methods offer flexible ways to handle stream processing and string concatenation in Java 8.

Comments