Stream.iterate() with predicate
Use a predicate to stop iteration — like a for-loop in stream form.
কোড তুলনা
✕ Java 8
Stream.iterate(1, n -> n * 2)
.limit(10)
.forEach(System.out::println);
// can't stop at a condition
✓ Java 9+
Stream.iterate(
1,
n -> n < 1000,
n -> n * 2
).forEach(IO::println);
// stops when n >= 1000
এই কোডে সমস্যা দেখছেন? আমাদের জানান।
কেন আধুনিক পদ্ধতি ভালো
Natural termination
Stop based on a condition, not an arbitrary limit.
For-loop equivalent
Same semantics as for(seed; hasNext; next).
No infinite stream risk
The predicate guarantees termination.
পুরনো পদ্ধতি
iterate + limit
আধুনিক পদ্ধতি
iterate(seed, pred, op)
JDK থেকে
9
কঠিনতা
মধ্যম
JDK সমর্থন
Stream.iterate() with predicate
উপলব্ধ
Widely available since JDK 9 (Sept 2017)
কীভাবে কাজ করে
The three-argument Stream.iterate(seed, hasNext, next) works like a for-loop: seed is the start, hasNext determines when to stop, and next produces the next value.
সম্পর্কিত ডকুমেন্টেশন
প্রমাণ