/** * Convert an {@link java.lang.Iterable} to a {@link java.util.stream.Stream} * The stream returned must be closed. * * @param iterable the input iterable * @param <T> the type of object stored in the iterable * @return a stream containing the contents of the iterable */ public static <T> Stream<T> toParallelStream(final Iterable<T> iterable) { if (iterable instanceof StreamIterable) { return ((StreamIterable<T>) iterable).getStream().parallel(); } return StreamSupport.stream(iterable.spliterator(), true) .onClose(() -> CloseableUtil.close(iterable)); }
/** * Convert an {@link java.lang.Iterable} to a {@link java.util.stream.Stream} * The stream returned must be closed. * * @param iterable the input iterable * @param <T> the type of object stored in the iterable * @return a stream containing the contents of the iterable */ public static <T> Stream<T> toStream(final Iterable<T> iterable) { if (iterable instanceof StreamIterable) { return ((StreamIterable<T>) iterable).getStream(); } return StreamSupport.stream(iterable.spliterator(), false) .onClose(() -> CloseableUtil.close(iterable)); }