This question already has an answer here:
- Limit a stream by a predicate 18 answers
I am writing a code to calculate Fibonacci numbers. With this code I can generate first n numbers of the Fibonacci sequence.
Stream.generate(new Supplier<Long>() {
private long n1 = 1;
private long n2 = 2;
@Override
public Long get() {
long fibonacci = n1;
long n3 = n2 + n1;
n1 = n2;
n2 = n3;
return fibonacci;
}
}).limit(50).forEach(System.out::println);
The method limit
returns the Stream
which holds the number of elements passed to this method. I want to stop the generation of the Stream
after the Fibonacci number reached some value.
I mean if I want to list all Fibonacci numbers less than 1000 then I cannot use limit
, because I don't know how many Fibonacci numbers there could be.
Is there any way to do this using lambda expressions?
If you don't mind using an iterator, you can write it as:
Yes, there is a lambda way but unfortunately, I don't think it is implemented in the current Java 8
Stream
API. Sorry to point you to a different language, but what I think you want is something likefrom the Scala Stream API.
As this is not implemented in the Java API, you have to do it yourself. How about this one:
Then you could use it like
(Assuming that
Stream
is an instance ofIterable
- if not, you might need to call the.iterator()
method and wrap that up)The best solution using the
Stream
’s built-in features I could find is:It has the disadvantage of processing the first item being
>=1000
though. This can be prevented by making the statement conditional, e.g.but I don’t like to evaluate the same condition (bigger than thousand or not) twice. But maybe one of these two solution might be practical enough for real life tasks where a limit based on the resulting value is needed.
I think, it’s clear that the entire construct is not parallel capable…
Dirty first version