Register a Stream “completion” hook
问题 Using the Java 8 Stream API, I would like to register a "completion hook", along the lines of: Stream<String> stream = Stream.of("a", "b", "c"); // additional filters / mappings that I don't control stream.onComplete((Completion c) -> { // This is what I'd like to do: closeResources(); // This might also be useful: Optional<Throwable> exception = c.exception(); exception.ifPresent(e -> throw new ExceptionWrapper(e)); }); The reason why I want to do that is because I want to wrap a resource in