Java Stream limit element number

Description

Java Stream limit element number

import java.util.stream.Stream;

public class Main {
  public static void main(String[] args) {
    Stream.iterate(2L, n  ->  n  + 1)
    .filter(Main::isOdd)//w ww .  j  a  v  a 2  s  .  c om
    .limit(20)
    .forEach(System.out::println);
  }
  public static boolean isOdd(long number) {
    if (number % 2 == 0) {
      return false;
    }
    return true;
  }
}



PreviousNext

Related