M
M
mitaichik2017-07-11 17:48:42
Java
mitaichik, 2017-07-11 17:48:42

Java Stream Api: how to get the number of elements without terminating the stream?

I have a stream:

Files.list(tmpDir)
      .filter(this::isCacheFileExpired)
      
      // здесь я хочу заллогировать сколько файлов будет удаленно
      
      .forEach(this::deleteCacheFile);

And in place of the comment, you need to embed a code that logs the number of deleted (that is, filtered) files.
What operator can do this?
Thanks in advance!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Zagaevsky, 2017-07-11
@mitaichik

It is impossible, this number will appear only after the end of the stream.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question