It seems IntStream.iterate
is just a lightweight ordered version for IntStream.generate
.
Is it true? If not, what is the core difference?
Note how their signatures are different:
generate
takes aIntSupplier
, which means that you are supposed to generate ints without being given anything. Example usages include creating a constant stream of the same integer, creating a stream of random integers. Notice how each element in the stream do not depend on the previous element.iterate
takes aseed
and aIntUnaryOperator
, which means that you are supposed to generate each element based on the previous element. This is useful for creating a inductively defined sequence, for example. In this case, each element is supposed to depend on the previous one.