How to use filter in IntStream?

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

Syntax:

Modifier and Type               Method
IntStream                       filter(IntPredicate predicate)
package com.nourit.java8.Streams.IntStream;

import java.util.stream.IntStream;

public class IntStreamFilter {

  public static void main(String[] args) {
    //Returns a stream consisting of the elements of this stream that match the given predicate.
    IntStream i1 = IntStream.of(1, 2, 3, 4, 5, 6, 7, 8);
    System.out.println("---------- Filter Example  -----------------");
      i1.filter(n -> n >= 4)
       .forEach(System.out::println);
      System.out.println("---------- Second Example  -----------------");
      IntStream i2 = IntStream.of(1, 2, 3, 4, 5, 6, 7, 8);
      i2.filter(n -> n < 4)
       .forEach(System.out::println);
    System.out.println("---------------------------------------");
  }
}

Output:

---------- Filter Example 1 -----------------
4
5
6
7
8
---------- Filter Example 2 -----------------
1
2
3
---------------------------------------

 

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.

*