Stream.iterate builds an infinite stream; limit makes it finite.
List<Integer> powers = Stream.iterate(1, n -> n * 2)
.limit(6)
.collect(Collectors.toList());
System.out.println(powers);
[1, 2, 4, 8, 16, 32]
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-26