java.util.Spliterators.AbstractSpliterator Java Examples
The following examples show how to use
java.util.Spliterators.AbstractSpliterator.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: BiStream.java From mug with Apache License 2.0 | 5 votes |
<T> Spliterator<T> ofObj(BiFunction<? super K, ? super V, ? extends T> mapper) { return new AbstractSpliterator<T>(estimateSize(), ORDERED) { @Override public boolean tryAdvance(Consumer<? super T> consumer) { return advance() && emit(mapper.apply(currentLeft.value, currentRight.value), consumer); } }; }
Example #2
Source File: MoreStreams.java From mug with Apache License 2.0 | 3 votes |
/** * Similar to {@link Stream#generate}, returns an infinite, sequential, unordered, and non-null * stream where each element is generated by the provided Supplier. The stream however will * terminate as soon as the Supplier returns null, in which case the null is treated as the * terminal condition and doesn't constitute a stream element. * * <p>For sequential iterations, {@code whileNotNll()} is usually more concise than implementing * {@link AbstractSpliterator} directly. The latter requires boilerplate that looks like this: * * <pre>{@code * return StreamSupport.stream( * new AbstractSpliterator<T>(MAX_VALUE, NONNULL) { * public boolean tryAdvance(Consumer<? super T> action) { * if (hasData) { * action.accept(data); * return true; * } * return false; * } * }, false); * }</pre> * * Which is equivalent to the following one-liner using {@code whileNotNull()}: * * <pre>{@code * return whileNotNull(() -> hasData ? data : null); * }</pre> * * <p>Why null? Why not {@code Optional}? Wrapping every generated element of a stream in an * {@link Optional} carries considerable allocation cost. Also, while nulls are in general * discouraged, they are mainly a problem for users who have to remember to deal with them. * The stream returned by {@code whileNotNull()} on the other hand is guaranteed to never include * nulls that users have to worry about. * * <p>If you already have an {@code Optional} from a method return value, you can use {@code * whileNotNull(() -> optionalReturningMethod().orElse(null))}. * * <p>One may still need to implement {@code AbstractSpliterator} or {@link java.util.Iterator} * directly if null is a valid element (usually discouraged though). * * <p>If you have an imperative loop over a mutable queue or stack: * * <pre>{@code * while (!queue.isEmpty()) { * int num = queue.poll(); * if (someCondition) { * ... * } * } * }</pre> * * it can be turned into a stream using {@code whileNotNull()}: * * <pre>{@code * whileNotNull(queue::poll).filter(someCondition)... * }</pre> * * @since 4.1 */ public static <T> Stream<T> whileNotNull(Supplier<? extends T> supplier) { requireNonNull(supplier); return StreamSupport.stream( new AbstractSpliterator<T>(Long.MAX_VALUE, Spliterator.NONNULL) { @Override public boolean tryAdvance(Consumer<? super T> action) { T element = supplier.get(); if (element == null) return false; action.accept(element); return true; } }, false); }