How to use concat in IntStream?

Here is an example to explain how to use concate in IntStream.

Syntax:

Modifier and Type         		Method
static IntStream                concat(IntStream a, IntStream b)
package com.nourit.java8.Streams.IntStream;

import java.util.stream.IntStream;

public class IntStreamConcat {

  public static void main(String[] args) {
    System.out.println("---------- Concat Example 1 -----------------");
    IntStream intStream1 = IntStream.of(0, 1, 2, 3, 4, 5, 6);
    IntStream intStream2 = IntStream.of(7, 8, 9, 10);
    //Creates a lazily concatenated stream whose elements are all the elements of 
    //the first stream followed by all the elements of the second stream.
    IntStream intStream = IntStream.concat(intStream1, intStream2);
    intStream.forEach(System.out::println);
    System.out.println("---------- Concat Example 2 -----------------");
    IntStream first = IntStream.builder().add(10).build();
      IntStream second = IntStream.builder().add(11).build();
      IntStream third = IntStream.concat(first, second);
      third.forEach(System.out::println);
    System.out.println("---------------------------------------");
  }
}

Output:

---------- Concat Example 1 -----------------
0
1
2
3
4
5
6
7
8
9
10
---------- Concat Example 2 -----------------
10
11
---------------------------------------

 

NOUR-IT

IT Training Center for Java and Web technology. If you enjoy and like our tutorials and believe in our mission, Please Subscribe us on Youtube and follow us on Twitter, or befriend us on Facebook or Google Plus.

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.

*