当前位置: 首页>>代码示例>>Java>>正文


Java Spliterators.AbstractIntSpliterator方法代码示例

本文整理汇总了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;
        }
    };
}
 
开发者ID:fralalonde,项目名称:iostream,代码行数:28,代码来源:StreamInputAdapter.java

示例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);
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:46,代码来源:IntStream.java


注:本文中的java.util.Spliterators.AbstractIntSpliterator方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。