Using the Java 8 Stream API, I would like to register a \"completion hook\", along the lines of:
Stream stream = Stream.of(\"a\",
Java 8 already has a precedent for how streams that need to be closed operate. In their Javadoc, it mentions:
Streams have a BaseStream.close() method and implement AutoCloseable, but nearly all stream instances do not actually need to be closed after use. Generally, only streams whose source is an IO channel (such as those returned by Files.lines(Path, Charset)) will require closing. Most streams are backed by collections, arrays, or generating functions, which require no special resource management. (If a stream does require closing, it can be declared as a resource in a try-with-resources statement.)
So Java 8's recommendation is to open those streams in a try-with-resources. And once you do that, Stream also provides a way for you to add a close hook, almost exactly as you've described: onClose(Runnable), which accepts a lambda telling it what to do and returns a Stream that will also do that operation when it is closed.
That's the way the API design and documentation suggests to do what you're trying to do.