本文整理汇总了Java中java.util.Spliterators.AbstractIntSpliterator方法的典型用法代码示例。如果您正苦于以下问题:Java Spliterators.AbstractIntSpliterator方法的具体用法?Java Spliterators.AbstractIntSpliterator怎么用?Java Spliterators.AbstractIntSpliterator使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.util.Spliterators
的用法示例。
在下文中一共展示了Spliterators.AbstractIntSpliterator方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: takeIntWhile
import java.util.Spliterators; //导入方法依赖的package包/类
/**
* Adapted from StackOverflow {@linkplain so http://stackoverflow.com/questions/20746429/limit-a-stream-by-a-predicate}
*
* @param splitr the original Spliterator
* @param predicate the predicate
* @return a Spliterator.OfInt
*/
private static Spliterator.OfInt takeIntWhile(Spliterator.OfInt splitr, IntPredicate predicate) {
return new Spliterators.AbstractIntSpliterator(splitr.estimateSize(), 0) {
boolean stillGoing = true;
@Override
public boolean tryAdvance(IntConsumer consumer) {
if (stillGoing) {
boolean hadNext = splitr.tryAdvance((int elem) -> {
if (predicate.test(elem)) {
consumer.accept(elem);
} else {
stillGoing = false;
}
});
return hadNext && stillGoing;
}
return false;
}
};
}
示例2: iterate
import java.util.Spliterators; //导入方法依赖的package包/类
/**
* Returns an infinite sequential ordered {@code IntStream} 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 IntStream} 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 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 IntStream}
*/
public static IntStream iterate(final int seed, final IntUnaryOperator f) {
Objects.requireNonNull(f);
Spliterator.OfInt spliterator = new Spliterators.AbstractIntSpliterator(Long.MAX_VALUE,
Spliterator.ORDERED | Spliterator.IMMUTABLE | Spliterator.NONNULL) {
int prev;
boolean started;
@Override
public boolean tryAdvance(IntConsumer action) {
Objects.requireNonNull(action);
int t;
if (started)
t = f.applyAsInt(prev);
else {
t = seed;
started = true;
}
action.accept(prev = t);
return true;
}
};
return StreamSupport.intStream(spliterator, false);
}