The javadoc of Spliterator
mentions that:
A Spliterator may traverse elements individually (tryAdvance()) or sequentially in bulk (forEachRemaining()).
Then we go to the javadoc of tryAdvance()
which says that:
If a remaining element exists, performs the given action on it, returning true; else returns false.
Maybe I am misreading somewhere, but to me it seems that provided there is one element, or more, remaining, the Consumer
as an argument should only every .accept()
one argument before returning true
, and that if, say, I have two arguments immediately available, then I cannot:
action.accept(arg1);
action.accept(arg2);
return true;
In this project, I have rewritten the breadth first spliterator so that it now reads:
// deque is a Deque<Iterator<T>>
@Override
public boolean tryAdvance(final Consumer<? super T> action)
{
Iterator<T> iterator;
T element;
while (!deque.isEmpty()) {
iterator = deque.removeFirst();
while (iterator.hasNext()) {
element = iterator.next();
deque.add(fn.apply(element));
action.accept(element);
}
}
return false;
}
In short, I make the action
accept all arguments, and then return false... and the test, albeit quite simple, still succeeds (link).
Note that .trySplit()
always returns null
; and the spliterator has chacacteristics DISTINCT
, ORDERED
and NONNULL
.
So, is there a stream usage in which the code above will not work due to the method above consuming all elements at once?