LongStream concat() method in Java

Java 8Object Oriented ProgrammingProgramming

The concat() method in the LongStream class creates a concatenated stream whose elements are all the elements of the first stream followed by all the elements of the second stream.

The syntax is as follows.

static LongStream concat(LongStream one, LongStream two)

Here, parameter one is the 1st stream, whereas two is the 2nd stream. The method returns the concatenation of both the streams.

To use the LongStream class in Java, import the following package.

import java.util.stream.LongStream;

Create a LongStrem and add some elements.

LongStream streamOne = LongStream.of(100L, 150L, 300L, 500L);

Now, create another stream.

LongStream streamTwo = LongStream.of(200L, 250L, 400L, 600L, 550L);

To concatenate both the above streams, use the concat() method.

LongStream.concat(streamOne, streamTwo)

The following is an example to implement LongStream concat() method in Java.

Example

 Live Demo

import java.util.stream.LongStream;
import java.util.stream.Stream;
public class Demo {
   public static void main(String[] args) {
      LongStream streamOne = LongStream.of(100L, 150L, 300L, 500L);
      LongStream streamTwo = LongStream.of(200L, 250L, 400L, 600L, 550L);
      System.out.println("Result of the Concatenated Streams...");
      LongStream.concat(streamOne, streamTwo).forEach(a -> System.out.println(a));
   }
}

Output

Result of the Concatenated Streams...
100
150
300
500
200
250
400
600
550
raja
Published on 27-Mar-2019 11:06:51
Advertisements