Actually range is ideal for this.
IntStream.range(0, 100).filter(x -> x % 3 == 0); //107,566 ns/op [Average]
Edit: Holgers's solution is the fastest performing solution.
Since the following lines of code
IntStream.range(0, 100).filter(x -> x % 3 == 0).forEach((x) -> x = x + 2);
IntStream.range(0, 100 / 3).map(x -> x * 3).forEach((x) -> x = x + 2);
int limit = ( 100 / 3 ) + 1;
IntStream.iterate(0, n -> n + 3).limit(limit).forEach((x) -> x = x + 2);
show these benchmark results
Benchmark Mode Cnt Score Error Units
Benchmark.intStreamTest avgt 5 485,473 ± 58,402 ns/op
Benchmark.intStreamTest2 avgt 5 202,135 ± 7,237 ns/op
Benchmark.intStreamTest3 avgt 5 280,307 ± 41,772 ns/op
Answer from JasperJ on Stack Overflow Top answer 1 of 7
22
Actually range is ideal for this.
IntStream.range(0, 100).filter(x -> x % 3 == 0); //107,566 ns/op [Average]
Edit: Holgers's solution is the fastest performing solution.
Since the following lines of code
IntStream.range(0, 100).filter(x -> x % 3 == 0).forEach((x) -> x = x + 2);
IntStream.range(0, 100 / 3).map(x -> x * 3).forEach((x) -> x = x + 2);
int limit = ( 100 / 3 ) + 1;
IntStream.iterate(0, n -> n + 3).limit(limit).forEach((x) -> x = x + 2);
show these benchmark results
Benchmark Mode Cnt Score Error Units
Benchmark.intStreamTest avgt 5 485,473 ± 58,402 ns/op
Benchmark.intStreamTest2 avgt 5 202,135 ± 7,237 ns/op
Benchmark.intStreamTest3 avgt 5 280,307 ± 41,772 ns/op
2 of 7
16
Actually you can also achieve the same results with a combination of peek and allMatch:
IntStream.iterate(0, n -> n + 3).peek(n -> System.out.printf("%d,", n)).allMatch(n -> n < 100 - 3);
This prints
0,3,6,9,12,15,18,21,24,27,30,33,36,39,42,45,48,51,54,57,60,63,66,69,72,75,78,81,84,87,90,93,96,99,
But nevertheless, this one is faster:
IntStream.range(0, 100 / 3 + 1).map(x -> x * 3).forEach((x) -> System.out.printf("%d,", x));
Java 9 Update:
Now the same iteration easier to achieve with Java 9:
Stream.iterate(0, i -> i <= 100, i -> 3 + i).forEach(i -> System.out.printf("%d,", i));
Oracle
docs.oracle.com › javase › 8 › docs › api › java › util › stream › IntStream.html
IntStream (Java Platform SE 8 )
3 weeks ago - Returns an infinite sequential ordered IntStream produced by iterative application of a function f to an initial element seed, producing a Stream consisting of seed, f(seed), f(f(seed)), etc.
Videos
Oracle
docs.oracle.com › javase › 9 › docs › api › java › util › stream › IntStream.html
IntStream (Java SE 9 & JDK 9 )
Returns a sequential ordered IntStream produced by iterative application of the given next function to an initial element, conditioned on satisfying the given hasNext predicate.
Tabnine
tabnine.com › home page › code › java › java.util.stream.intstream
java.util.stream.IntStream.iterate java code examples | Tabnine
public static String[] chop(String input, int step) { if (input == null || input.length() == 0) { return EMPTY_ARRAY; } if (step == 0) { return new String[]{input}; } int strLength = input.length(); int iterations = strLength % step == 0 ? strLength / step : strLength / step + 1; return IntStream.iterate(0, i -> i + step) .limit(iterations) .mapToObj(i -> input.substring(i, (i + step) < strLength ?
Oracle
docs.oracle.com › en › java › javase › 21 › docs › api › java.base › java › util › stream › IntStream.html
IntStream (Java SE 21 & JDK 21)
January 20, 2026 - Returns a sequential ordered IntStream produced by iterative application of the given next function to an initial element, conditioned on satisfying the given hasNext predicate.
Oracle
docs.oracle.com › en › java › javase › 11 › docs › api › java.base › java › util › stream › IntStream.html
IntStream (Java SE 11 & JDK 11 )
January 20, 2026 - Returns a sequential ordered IntStream produced by iterative application of the given next function to an initial element, conditioned on satisfying the given hasNext predicate.
GeeksforGeeks
geeksforgeeks.org › java › intstream-iterator-method-in-java
IntStream iterator() Method in Java - GeeksforGeeks
May 7, 2025 - This avoids the need for converting the primitive types into objects. The iterator() method in IntStream is a terminal operation that gives us an iterator, and with the help of this iterator, we can iterate over the elements of the stream.
Oracle
docs.oracle.com › en › java › javase › 17 › docs › api › java.base › java › util › stream › IntStream.html
IntStream (Java SE 17 & JDK 17)
January 20, 2026 - Returns a sequential ordered IntStream produced by iterative application of the given next function to an initial element, conditioned on satisfying the given hasNext predicate.
Oracle
docs.oracle.com › en › java › javase › 26 › docs › api › java.base › java › util › stream › IntStream.html
IntStream (Java SE 26 & JDK 26)
3 weeks ago - Returns a sequential ordered IntStream produced by iterative application of the given next function to an initial element, conditioned on satisfying the given hasNext predicate.
GeeksforGeeks
geeksforgeeks.org › intstream-iterator-java
IntStream iterator() in Java - GeeksforGeeks
March 21, 2018 - IntStream range(int startInclusive, ... Parameters : IntStream : A sequence of primitive ... Given an Iterator, the task is to convert it into Iterables in Java....
TutorialsPoint
tutorialspoint.com › intstream-iterator-method-in-java
IntStream iterator() method in Java
import java.util.*; import java.util.stream.IntStream; public class Demo { public static void main(String[] args) { IntStream intStream = IntStream.of(15, 40, 55, 70, 95, 120); PrimitiveIterator.OfInt primIterator = intStream.iterator(); while (primIterator.hasNext()) { System.out.println(primIterator.nextInt()); } } }
Java Code Geeks
javacodegeeks.com › home › core java
Java 8 IntStream With Working Examples - Java Code Geeks
January 3, 2022 - In this tutorial, We’ll learn how to use the IntStream in java 8 and it uses with example programs.
HowToDoInJava
howtodoinjava.com › home › java 8 › guide to intstream in java
Guide to IntStream in Java - HowToDoInJava
March 4, 2022 - The generate() method looks a lot like iterator(), but differs by not calculating the int values by incrementing the previous value. Rather an IntSupplier is provided which is a functional interface is used to generate an infinite sequential unordered stream of int values. Following example create a stream of 10 random numbers and then print them in the console. IntStream stream = IntStream .generate(() -> { return (int)(Math.random() * 10000); }); stream.limit(10).forEach(System.out::println);
Baeldung
baeldung.com › home › java › java streams › java intstream conversions
Java IntStream Conversions | Baeldung
January 8, 2024 - First, let’s create an infinite stream of integers starting at 0 and iterating by adding 2 to each element. Immediately after that, we need to add an intermediate operation limit in order to make this operation, somehow, terminating. Finally, let’s use the terminating operation collect to gather this Stream to an array. This is a straight-forward way of generating an array of ints. Let’s convert now an IntStream to a List of Integers.
Oracle
docs.oracle.com › javase › 10 › docs › api › java › util › stream › IntStream.html
IntStream (Java SE 10 & JDK 10 )
Returns a sequential ordered IntStream produced by iterative application of the given next function to an initial element, conditioned on satisfying the given hasNext predicate.
Straub
straub.as › java › history › java8 › intstreams.html
IntStreams
IntConsumer printInt = i -> System.out.print(i + " "); IntFunction<? extends IntStream> mapper = i -> IntStream.iterate(i, k -> k).limit(3) ; IntStream sequence = IntStream.rangeClosed(1, 5).flatMap(mapper); sequence.forEach(printInt);