Providing Limit condition on Stream generation [duplicate]
问题 This question already has answers here : Limit a stream by a predicate (19 answers) Closed 5 years ago . 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