Java LongStream concat() Method

The concat() method in Java, part of the java.util.stream.LongStream interface, is used to concatenate two LongStream instances. This method is useful when you need to combine multiple streams into a single stream.

Table of Contents

  1. Introduction
  2. concat() Method Syntax
  3. Understanding concat()
  4. Examples
    • Basic Usage
    • Using concat() with Filtered Streams
  5. Real-World Use Case
  6. Conclusion

Introduction

The concat() method concatenates two streams, creating a lazy concatenation that preserves the order of elements from both streams. The resulting stream contains all the elements of the first stream followed by all the elements of the second stream.

concat() Method Syntax

The syntax for the concat() method is as follows:

static LongStream concat(LongStream a, LongStream b)

Parameters:

  • a: The first LongStream to be concatenated.
  • b: The second LongStream to be concatenated.

Returns:

  • A LongStream that contains all elements of the first stream followed by all elements of the second stream.

Throws:

  • This method does not throw any exceptions.

Understanding concat()

The concat() method provides a way to combine two streams into one. The resulting stream is a lazy concatenation, meaning that elements are only combined when the stream is processed. This ensures efficient memory usage and allows for further stream operations to be applied seamlessly.

Examples

Basic Usage

To demonstrate the basic usage of concat(), we will create two LongStream instances and concatenate them.

Example

import java.util.stream.LongStream;

public class ConcatExample {
    public static void main(String[] args) {
        LongStream stream1 = LongStream.of(1L, 2L, 3L);
        LongStream stream2 = LongStream.of(4L, 5L, 6L);

        // Use concat() to concatenate the two streams
        LongStream concatenatedStream = LongStream.concat(stream1, stream2);

        // Print the elements of the concatenated stream
        concatenatedStream.forEach(System.out::println);
    }
}

Output:

1
2
3
4
5
6

Using concat() with Filtered Streams

This example shows how to use concat() in combination with filtered streams to create a single stream of specific elements.

Example

import java.util.stream.LongStream;

public class ConcatWithFilterExample {
    public static void main(String[] args) {
        LongStream stream1 = LongStream.of(1L, 2L, 3L, 4L, 5L).filter(n -> n % 2 == 0);
        LongStream stream2 = LongStream.of(6L, 7L, 8L, 9L, 10L).filter(n -> n > 7);

        // Use concat() to concatenate the filtered streams
        LongStream concatenatedStream = LongStream.concat(stream1, stream2);

        // Print the elements of the concatenated stream
        concatenatedStream.forEach(System.out::println);
    }
}

Output:

2
4
8
9
10

Real-World Use Case

Concatenating Streams of Transaction Amounts

In real-world applications, the concat() method can be used to concatenate streams of transaction amounts from different sources into a single stream for unified processing.

Example

import java.util.stream.LongStream;

public class ConcatTransactionsExample {
    public static void main(String[] args) {
        LongStream onlineTransactions = LongStream.of(1000L, 2000L, 1500L);
        LongStream inStoreTransactions = LongStream.of(3000L, 2500L, 4000L);

        // Use concat() to concatenate the streams of transaction amounts
        LongStream allTransactions = LongStream.concat(onlineTransactions, inStoreTransactions);

        // Print the elements of the concatenated stream
        allTransactions.forEach(System.out::println);
    }
}

Output:

1000
2000
1500
3000
2500
4000

Conclusion

The LongStream.concat() method is used to concatenate two LongStream instances, creating a single stream that contains all elements of the first stream followed by all elements of the second stream. This method is particularly useful for combining streams from different sources into a unified stream for further processing. By understanding and using this method, you can efficiently manage and process streams of values in your Java applications, ensuring that elements are concatenated as needed.

Comments