First idea that may come to mind would be to truncate the stream with limit
:
Stream.iterate(foo1, Foo::getNext)
.limit(size)....
but obviously, this is not useful if the size is unknown.
if the size is unknown then surely there must be some type of condition to say "stop and no more".
JDK-9 offers these overloads:
Stream.iterate(foo1, Foo::getNext)
.takeWhile(f -> someCondition)
.... // further intermediate or a terminal operation
or:
Stream.iterate(foo1, f -> someCondition, Foo::getNext)
.... // further intermediate or a terminal operation
if you cannot use JDK-9 then your only other option is to create your own helper method to perform the logic you're after.