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


Java Spliterators.AbstractSpliterator方法代码示例

本文整理汇总了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;
    }
  };
}
 
开发者ID:zugzug90,项目名称:guava-mock,代码行数:23,代码来源:ImmutableSortedSet.java

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


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