當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。