How to use count in IntStream?

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

Syntax:

Modifier and Type               Method
long                            count()
package com.nourit.java8.Streams.IntStream;

import java.util.stream.IntStream;

public class IntStreamCount {

  public static void main(String[] args) {
    System.out.println("---------- Count Example 1 -----------------");
    //Returns the count of elements in this stream.
    long count1 = IntStream.of(0, 1, 2, 3, 4, 5, 6).count();
    System.out.println("count1 = " + count1);
    long count2 = IntStream.of(7, 8, 9, 10).count();
    System.out.println("count2 = " + count2);
    System.out.println("---------- Count Example 2 -----------------");
    IntStream first = IntStream.builder().add(10).build();
      IntStream second = IntStream.builder().add(11).build();
      long count3 = IntStream.concat(first, second).count();
      System.out.println("count3 = " + count3);
    System.out.println("---------------------------------------");
  }
}

Output:

---------- Count Example 1 -----------------
count1 = 7
count2 = 4
---------- Count Example 2 -----------------
count3 = 2
---------------------------------------

 

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.

*