Similar to this question: Java Streams — How to perform an intermediate function every nth item
But this time, always do something intermediate on the last terminal value retrieved. Something akin to this:
IntStream.rangeClosed(1, 26)
.filter(it -> isPrime(it))
.peek(every(5, System.out::println))
.onFinalValue( it -> System.out.println("Last value is: " + it))
.forEach( it -> {});
That way it should print the values every 5th one, but also show the very last one.
You may, or may not, know the length of the original stream. So it may be hot (live audio, tv video feed) or cold (pre-recorded audio, or other forward/backward seekable source).