本文整理匯總了Java中java.util.Spliterators.AbstractSpliterator方法的典型用法代碼示例。如果您正苦於以下問題:Java Spliterators.AbstractSpliterator方法的具體用法?Java Spliterators.AbstractSpliterator怎麽用?Java Spliterators.AbstractSpliterator使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類java.util.Spliterators
的用法示例。
在下文中一共展示了Spliterators.AbstractSpliterator方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: spliterator
import java.util.Spliterators; //導入方法依賴的package包/類
@Override
public Spliterator<E> spliterator() {
return new Spliterators.AbstractSpliterator<E>(
size(), SPLITERATOR_CHARACTERISTICS | Spliterator.SIZED) {
final UnmodifiableIterator<E> iterator = iterator();
@Override
public boolean tryAdvance(Consumer<? super E> action) {
if (iterator.hasNext()) {
action.accept(iterator.next());
return true;
} else {
return false;
}
}
@Override
public Comparator<? super E> getComparator() {
return comparator;
}
};
}
示例2: iterate
import java.util.Spliterators; //導入方法依賴的package包/類
/**
* Returns an infinite sequential ordered {@code Stream} produced by iterative
* application of a function {@code f} to an initial element {@code seed},
* producing a {@code Stream} consisting of {@code seed}, {@code f(seed)},
* {@code f(f(seed))}, etc.
*
* <p>The first element (position {@code 0}) in the {@code Stream} will be
* the provided {@code seed}. For {@code n > 0}, the element at position
* {@code n}, will be the result of applying the function {@code f} to the
* element at position {@code n - 1}.
*
* <p>The action of applying {@code f} for one element
* <a href="../concurrent/package-summary.html#MemoryVisibility"><i>happens-before</i></a>
* the action of applying {@code f} for subsequent elements. For any given
* element the action may be performed in whatever thread the library
* chooses.
*
* @param <T> the type of stream elements
* @param seed the initial element
* @param f a function to be applied to the previous element to produce
* a new element
* @return a new sequential {@code Stream}
*/
public static<T> Stream<T> iterate(final T seed, final UnaryOperator<T> f) {
Objects.requireNonNull(f);
Spliterator<T> spliterator = new Spliterators.AbstractSpliterator<>(Long.MAX_VALUE,
Spliterator.ORDERED | Spliterator.IMMUTABLE) {
T prev;
boolean started;
@Override
public boolean tryAdvance(Consumer<? super T> action) {
Objects.requireNonNull(action);
T t;
if (started)
t = f.apply(prev);
else {
t = seed;
started = true;
}
action.accept(prev = t);
return true;
}
};
return StreamSupport.stream(spliterator, false);
}