LongStream concat() in Java

LongStream concat() method creates a concatenated stream in which the elements are all the elements of the first stream followed by all the elements of the second stream. The resulting stream is ordered if both of the input streams are ordered, and parallel if either of the input streams is parallel.

Syntax :

static LongStream concat(LongStream a, LongStream b)

Parameters :



  1. LongStream : A sequence of primitive long-valued elements.
  2. a : Represents the first stream.
  3. b : Represents the second stream.

Return Value : The function returns the concatenation of the two input LongStreams.

The calls to LongStream.concat(LongStream a, LongStream b) can be think of as forming a binary tree. The concatenation of all the input streams is at the root. The individual input streams are at the leaves. Below given is example for 3 LongStreams a, b and c.

Each additional input stream adds one layer of depth to the tree and one layer of indirection to reach all the other streams.

Note : The elements returned by LongStream.concat() method is ordered. For example, the following two lines returns the same result:

LongStream.concat(LongStream.concat(stream1, stream2), stream3);
LongStream.concat(stream1, LongStream.concat(stream2, stream3));

But the result for the following two are different.

LongStream.concat(LongStream.concat(stream1, stream2), stream3); 
LongStream.concat(LongStream.concat(stream2, stream1), stream3);

Example 1 :

filter_none

edit
close

play_arrow

link
brightness_4
code

// Implementation of LongStream concat()
// method in Java 8 with 2 LongStreams
import java.util.*;
import java.util.stream.LongStream;
import java.util.stream.Stream;
  
class GFG {
  
    // Driver code
    public static void main(String[] args)
    {
        // Creating two LongStreams
        LongStream stream1 = LongStream.of(2L, 4L, 6L);
        LongStream stream2 = LongStream.of(1L, 3L, 5L);
  
        // concatenating both the Streams
        // with LongStream concat() function
        // and displaying the result
        LongStream.concat(stream1, stream2)
            .forEach(element -> System.out.println(element));
    }
}

chevron_right


Output:

2
4
6
1
3
5

Example 2 :

filter_none

edit
close

play_arrow

link
brightness_4
code

// Implementation of LongStream concat()
// method in Java 8 with 2 LongStreams
import java.util.*;
import java.util.stream.LongStream;
import java.util.stream.Stream;
  
class GFG {
  
    // Driver code
    public static void main(String[] args)
    {
        // Creating two LongStreams
        LongStream stream1 = LongStream.of(2L, 4L, 6L);
        LongStream stream2 = LongStream.of(1L, 2L, 4L);
  
        // concatenating both the Streams
        // with LongStream concat() function
        // and displaying distinct elements
        // in the concatenated LongStream
        LongStream.concat(stream1, stream2).distinct().
             forEach(element -> System.out.println(element));
    }
}

chevron_right


Output:

2
4
6
1


My Personal Notes arrow_drop_up

In love with a semicolon because sometimes i miss it so badly)

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.