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 )
1 month 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 )
IntStream.iterate should produce the same sequence of elements as produced by the corresponding for-loop:
Java Code Geeks
javacodegeeks.com › home › core java
Java 8 IntStream With Working Examples - Java Code Geeks
January 3, 2022 - A quick guide to understand primitive int representation of Stream as interface IntStream to support integer operations and with the useful examples. In this tutorial, We’ll learn how to use the IntStream in java 8 and it uses with example programs.
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.
Baeldung
baeldung.com › home › java › java streams › how to iterate over a stream with indices
How to Iterate Over a Stream With Indices | Baeldung
2 weeks ago - Let’s implement a method which iterates with indices and demonstrates this approach. Simply put, we want to get an array of Strings and only select even indexed elements: public List<String> getEvenIndexedStrings(String[] names) { List<String> evenIndexedNames = IntStream .range(0, names.length) .filter(i -> i % 2 == 0) .mapToObj(i -> names[i]) .collect(Collectors.toList()); return evenIndexedNames; }
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 › intstream-iterator-java
IntStream iterator() in Java - GeeksforGeeks
March 21, 2018 - Examples: Input: Iterator = {1, 2, 3, 4, 5} Output: {1, 2, 3, 4, 5} Input: Iterator = {'G', 'e', 'e', 'k', 's'} Output: {'G', 'e', 'e', 'k', 's'} Below are the various ways to do so: By overriding the abstract method Iterable.itera
Medium
medium.com › @ilakk2023 › 245-iterating-in-reverse-direction-using-intstream-950663d55f54
245. Iterating in reverse direction using IntStream. | by Ilakkuvaselvi (Ilak) Manoharan | Medium
April 30, 2024 - However, for larger ranges or more complex iteration requirements, IntStream.iterate provides better efficiency and flexibility. Here is an example of using IntStream: import java.util.Arrays; import java.util.HashSet; import java.util.Set; import java.util.function.BiPredicate; import java.util.stream.IntStream; public class Pet3X3Team8 { public static boolean checkConstraints(int val, int x, int y) { return val >= x && val <= y; } public static int countTeamsEast(String set, int x, int y) { Set<int[]> positions = new HashSet<>(); // Define a BiPredicate to check constraints BiPredicate<Integ
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.
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(...
GeeksforGeeks
geeksforgeeks.org › java › intstream-foreach-method-java
IntStream forEach() Method in Java - GeeksforGeeks
May 6, 2025 - // Java program to demonstrate // IntStream.forEach on a range import java.util.stream.IntStream; public class Geeks { public static void main(String[] args) { // Creating an IntStream from 4 to 8 (9 is exclusive) IntStream stream = IntStream.ran...
Mkyong
mkyong.com › home › java8 › java 8 stream.iterate examples
Java 8 Stream.iterate examples - Mkyong.com
November 29, 2018 - int sum = Stream.iterate(new int[]{0, 1}, n -> new int[]{n[1], n[0] + n[1]}) .limit(10) .map(n -> n[0]) // Stream<Integer> .mapToInt(n -> n) .sum(); System.out.println("Fibonacci 10 sum : " + sum);