Is there any way to reuse a Stream?
问题 I'm learning the new Java 8 features, and while experimenting with streams ( java.util.stream.Stream ) and collectors, I realized that a stream can't be used twice. Is there any way to reuse it? 回答1: If you want to have the effect of reusing a stream, you might wrap the stream expression in a Supplier and call myStreamSupplier.get() whenever you want a fresh one. For example: Supplier<Stream<String>> sup = () -> someList.stream(); List<String> nonEmptyStrings = sup.get().filter(s -> !s