Java 9 gave Stream.iterate a predicate, so a bounded sequence no longer needs a separate limit() call.
List<Integer> powers = Stream.iterate(1, n -> n <= 64, n -> n * 2).toList();
System.out.println(powers);
String countdown = Stream.iterate(5, n -> n > 0, n -> n - 1)
.map(String::valueOf)
.collect(Collectors.joining(" -> "));
System.out.println(countdown);
[1, 2, 4, 8, 16, 32, 64]
5 -> 4 -> 3 -> 2 -> 1
Run this yourself in the Online Java Compiler, spin up a live REST API in the API Sandbox, or practise with Java interview questions.
Published 2026-07-30