IntStream concat() method in Java


The concat() method in the Java IntStream class forms a concatenated stream. The elements of this stream are all the elements of the first stream followed by all the elements of the second stream.

The syntax is as follows −

static IntStream concat(IntStream one, IntStream two)

Here, the parameter one is the first stream, whereas two is the second stream. The method returns the concatenated result of the stream one and two.

Let us create two IntStream and add some elements −

IntStream intStream1 = IntStream.of(10, 20, 30, 40, 50);
IntStream intStream2 = IntStream.of(60, 70, 80, 90);

Now, to concate both the streams, use the concat() method −

IntStream.concat(intStream1, intStream2)

The following is an example to implement IntStream concat() method in Java −

Example

 Live Demo

import java.util.stream.IntStream;
import java.util.stream.Stream;

public class Demo {
   public static void main(String[] args) {
      IntStream intStream1 = IntStream.of(10, 20, 30, 40, 50);
      IntStream intStream2 = IntStream.of(60, 70, 80, 90);
      // Concatenated stream
      System.out.println("Concatenated Stream...");
      IntStream.concat(intStream1, intStream2).forEach(element -> System.out.println(element));
   }
}

Output

Concatenated Stream...
10
20
30
40
50
60
70
80
90

Updated on: 30-Jul-2019

164 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements