Java 8 Program to Concatenate Two Streams

1. Introduction

Concatenating streams is a common operation when working with sequences of elements in Java, especially with the introduction of the Stream API in Java 8. This feature enhances the ability to perform complex operations on collections in a functional style, making code more readable and concise. Concatenation of streams involves combining the elements of two streams into a single stream. This is particularly useful in scenarios where data from different sources needs to be processed in a unified manner. This blog post will demonstrate how to concatenate two streams in Java 8, showcasing the simplicity and flexibility of stream operations.

Java 8 Program to Concatenate Two Streams

2. Program Steps

1. Create two streams of elements.

2. Use the Stream.concat() method to concatenate the two streams.

3. Collect the concatenated stream into a list or another collection as needed.

4. Display the result of the concatenation.

3. Code Program

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

public class ConcatenateStreams {
    public static void main(String[] args) {
        // Step 1: Creating two streams
        Stream<String> stream1 = Stream.of("Java", "Python", "C++");
        Stream<String> stream2 = Stream.of("JavaScript", "TypeScript", "Kotlin");

        // Step 2: Concatenating the two streams
        Stream<String> concatenatedStream = Stream.concat(stream1, stream2);

        // Step 3: Collecting the concatenated stream into a list
        List<String> resultList = concatenatedStream.collect(Collectors.toList());

        // Step 4: Displaying the result
        System.out.println("Concatenated Stream Result: " + resultList);
    }
}

Output:

Concatenated Stream Result: [Java, Python, C++, JavaScript, TypeScript, Kotlin]

Explanation:

1. The program begins by creating two Stream<String> instances, stream1 and stream2, each initialized with a set of string elements. These streams represent separate sequences of programming language names.

2. The Stream.concat() static method is used to concatenate stream1 and stream2 into a single stream, concatenatedStream. This method takes two streams as input and returns a new stream, which is the concatenation of the input streams, preserving the order of elements.

3. The concatenated stream is then collected into a List<String> using the collect(Collectors.toList()) terminal operation. This step gathers all elements from the stream into a list, allowing for easy access and manipulation of the combined elements.

4. Finally, the concatenated list of elements is printed to the console, showing the merged sequence of programming language names from both original streams. The output confirms that the elements of stream1 and stream2 have been successfully concatenated in the order they were provided, demonstrating an effective use of Java 8's Stream API to perform stream concatenation.

Comments