Java Stream filter pipelined

Description

Java Stream filter pipelined



import java.util.ArrayList;
import java.util.stream.Stream;

public class Main {

  public static void main(String[] args) {

    // Create a list of Integer values.
    ArrayList<Integer> myList = new ArrayList<>();
    myList.add(3);//from   w  w w  .ja v  a 2  s.c  om
    myList.add(18);
    myList.add(2);
    myList.add(25);
    myList.add(75);
    myList.add(15);

    System.out.println("Original list: " + myList);

    // Display only the odd values that are greater than 5. Notice that
    // two filter operations are pipelined.
    Stream<Integer> oddVals = myList.stream().filter((n) -> (n % 2) == 1).filter((n) -> n > 5);
    System.out.print("Odd values greater than 5: ");
    oddVals.forEach((n) -> System.out.print(n + " "));
    System.out.println();
  }
}



PreviousNext

Related