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


Java Spliterator.tryAdvance方法代码示例

本文整理汇总了Java中java.util.Spliterator.tryAdvance方法的典型用法代码示例。如果您正苦于以下问题:Java Spliterator.tryAdvance方法的具体用法?Java Spliterator.tryAdvance怎么用?Java Spliterator.tryAdvance使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在java.util.Spliterator的用法示例。


在下文中一共展示了Spliterator.tryAdvance方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: testSplitAfterFullTraversal

import java.util.Spliterator; //导入方法依赖的package包/类
private static <T, S extends Spliterator<T>> void testSplitAfterFullTraversal(
        Supplier<S> supplier,
        UnaryOperator<Consumer<T>> boxingAdapter) {
    // Full traversal using tryAdvance
    Spliterator<T> spliterator = supplier.get();
    while (spliterator.tryAdvance(boxingAdapter.apply(e -> { }))) { }
    Spliterator<T> split = spliterator.trySplit();
    assertNull(split);

    // Full traversal using forEach
    spliterator = supplier.get();
    spliterator.forEachRemaining(boxingAdapter.apply(e -> { }));
    split = spliterator.trySplit();
    assertNull(split);

    // Full traversal using tryAdvance then forEach
    spliterator = supplier.get();
    spliterator.tryAdvance(boxingAdapter.apply(e -> { }));
    spliterator.forEachRemaining(boxingAdapter.apply(e -> { }));
    split = spliterator.trySplit();
    assertNull(split);
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:23,代码来源:SpliteratorTestHelper.java

示例2: distinct

import java.util.Spliterator; //导入方法依赖的package包/类
public static <T> Stream<T> distinct(Stream<T> src, Comparator<T> cmp) {
    Spliterator<T> iter = src.spliterator();
    Spliterator<T> res = new AbstractSpliterator<T>(Long.MAX_VALUE, Spliterator.ORDERED ) {
        // ArrayList<T> distinctData = new ArrayList<>();
        TreeSet<T> distinctData = new TreeSet<>(cmp);
        @Override
        public boolean tryAdvance(Consumer<? super T> action) {
            return iter.tryAdvance( item -> {
                // Versão 1: if (!contains(distinctData, cmp, item)) {
                // Versão 2: if(!distinctData.stream().anyMatch(e -> cmp.compare(e, item) == 0)) {
                // Versão 3:
                if (!distinctData.contains(item)) {
                    distinctData.add(item);
                    action.accept(item);
                }
            });
        }
    };
    return StreamSupport.stream(res, false);
}
 
开发者ID:isel-leic-mpd,项目名称:mpd-2017-i41d,代码行数:21,代码来源:StreamUtils.java

示例3: mixedTraverseAndSplit

import java.util.Spliterator; //导入方法依赖的package包/类
static<U> void mixedTraverseAndSplit(Consumer<U> b, Spliterator<U> splTop) {
    Spliterator<U> spl1, spl2, spl3;
    splTop.tryAdvance(b);
    spl2 = splTop.trySplit();
    if (spl2 != null) {
        spl2.tryAdvance(b);
        spl1 = spl2.trySplit();
        if (spl1 != null) {
            spl1.tryAdvance(b);
            spl1.forEachRemaining(b);
        }
        spl2.tryAdvance(b);
        spl2.forEachRemaining(b);
    }
    splTop.tryAdvance(b);
    spl3 = splTop.trySplit();
    if (spl3 != null) {
        spl3.tryAdvance(b);
        spl3.forEachRemaining(b);
    }
    splTop.tryAdvance(b);
    splTop.forEachRemaining(b);
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:24,代码来源:SpliteratorTestHelper.java

示例4: testSplitAfterFullTraversal

import java.util.Spliterator; //导入方法依赖的package包/类
private static <T, S extends Spliterator<T>> void testSplitAfterFullTraversal(
        Supplier<S> supplier,
        UnaryOperator<Consumer<T>> boxingAdapter) {
    // Full traversal using tryAdvance
    Spliterator<T> spliterator = supplier.get();
    while (spliterator.tryAdvance(boxingAdapter.apply(e -> { }))) { }
    Spliterator<T> split = spliterator.trySplit();
    assertNull(split);

    // Full traversal using forEach
    spliterator = supplier.get();
    spliterator.forEachRemaining(boxingAdapter.apply(e -> {
    }));
    split = spliterator.trySplit();
    assertNull(split);

    // Full traversal using tryAdvance then forEach
    spliterator = supplier.get();
    spliterator.tryAdvance(boxingAdapter.apply(e -> { }));
    spliterator.forEachRemaining(boxingAdapter.apply(e -> {
    }));
    split = spliterator.trySplit();
    assertNull(split);
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:25,代码来源:SpliteratorCollisions.java

示例5: truncate

import java.util.Spliterator; //导入方法依赖的package包/类
/**
 * Return a node describing a subsequence of the elements of this node,
 * starting at the given inclusive start offset and ending at the given
 * exclusive end offset.
 *
 * @param from The (inclusive) starting offset of elements to include, must
 *             be in range 0..count().
 * @param to The (exclusive) end offset of elements to include, must be
 *           in range 0..count().
 * @param generator A function to be used to create a new array, if needed,
 *                  for reference nodes.
 * @return the truncated node
 */
default Node<T> truncate(long from, long to, IntFunction<T[]> generator) {
    if (from == 0 && to == count())
        return this;
    Spliterator<T> spliterator = spliterator();
    long size = to - from;
    Node.Builder<T> nodeBuilder = Nodes.builder(size, generator);
    nodeBuilder.begin(size);
    for (int i = 0; i < from && spliterator.tryAdvance(e -> { }); i++) { }
    if (to == count()) {
        spliterator.forEachRemaining(nodeBuilder);
    } else {
        for (int i = 0; i < size && spliterator.tryAdvance(nodeBuilder); i++) { }
    }
    nodeBuilder.end();
    return nodeBuilder.build();
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:30,代码来源:Node.java

示例6: forEach

import java.util.Spliterator; //导入方法依赖的package包/类
@Override
<E> void forEach(Spliterator<E> spliterator, Consumer<? super E> consumer) {
  while (spliterator.tryAdvance(consumer)) {
    Spliterator<E> prefix = trySplitTestingSize(spliterator);
    if (prefix != null) {
      forEach(prefix, consumer);
    }
  }
}
 
开发者ID:zugzug90,项目名称:guava-mock,代码行数:10,代码来源:SpliteratorTester.java

示例7: concat

import java.util.Spliterator; //导入方法依赖的package包/类
private static <T> Stream<T> concat(Stream<T> first, Stream<T> second) {
    Spliterator<T> iter1 = first.spliterator();
    Spliterator<T> iter2 = second.spliterator();
    Spliterator<T> iter = new AbstractSpliterator<T>(Long.MAX_VALUE, Spliterator.ORDERED) {
        boolean firstFinished = false;
        @Override
        public boolean tryAdvance(Consumer<? super T> action) {
            if(!firstFinished && iter1.tryAdvance(action)) return true;
            firstFinished = true;
            return iter2.tryAdvance(action);
        }
    };
    return StreamSupport.stream(iter, false);
}
 
开发者ID:isel-leic-mpd,项目名称:mpd-2017-i41d,代码行数:15,代码来源:App.java

示例8: filterEvenLine

import java.util.Spliterator; //导入方法依赖的package包/类
public static <T> Stream<T> filterEvenLine(Stream<T> src) {
    Spliterator<T> iter = src.spliterator();
    Spliterator<T> res = new AbstractSpliterator<T>(Long.MAX_VALUE, Spliterator.ORDERED ) {
        @Override
        public boolean tryAdvance(Consumer<? super T> action) {
            return iter.tryAdvance(item -> {})
                    ? iter.tryAdvance(action)
                    : false;
        }
    };
    return StreamSupport.stream(res, false);
}
 
开发者ID:isel-leic-mpd,项目名称:mpd-2017-i41d,代码行数:13,代码来源:StreamUtils.java

示例9: lateBindingTestWithTryAdvance

import java.util.Spliterator; //导入方法依赖的package包/类
@Test(dataProvider = "Source")
public <T> void lateBindingTestWithTryAdvance(String description, Supplier<Source<T>> ss) {
    Source<T> source = ss.get();
    Collection<T> c = source.asCollection();
    Spliterator<T> s = c.spliterator();

    source.update();

    Set<T> r = new HashSet<>();
    while (s.tryAdvance(r::add)) { }

    assertEquals(r, new HashSet<>(c));
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:14,代码来源:SpliteratorLateBindingFailFastTest.java

示例10: testTryAdvance

import java.util.Spliterator; //导入方法依赖的package包/类
@Test(dataProvider = "Source")
public <T> void testTryAdvance(String description, Supplier<Source<T>> ss) {
    Source<T> source = ss.get();
    Spliterator<T> s = source.spliterator();

    source.update();

    Set<T> a = new HashSet<>();
    while (s.tryAdvance(a::add)) {
    }

    Set<T> e = new HashSet<>();
    source.spliterator().forEachRemaining(e::add);
    assertEquals(a, e);
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:16,代码来源:SpliteratorLateBindingTest.java

示例11: filter

import java.util.Spliterator; //导入方法依赖的package包/类
/** Returns a {@code Spliterator} filtered by the specified predicate. */
static <T> Spliterator<T> filter(Spliterator<T> fromSpliterator, Predicate<? super T> predicate) {
  checkNotNull(fromSpliterator);
  checkNotNull(predicate);
  class Splitr implements Spliterator<T>, Consumer<T> {
    T holder = null;

    @Override
    public void accept(T t) {
      this.holder = t;
    }

    @Override
    public boolean tryAdvance(Consumer<? super T> action) {
      while (fromSpliterator.tryAdvance(this)) {
        try {
          if (predicate.test(holder)) {
            action.accept(holder);
            return true;
          }
        } finally {
          holder = null;
        }
      }
      return false;
    }

    @Override
    public Spliterator<T> trySplit() {
      Spliterator<T> fromSplit = fromSpliterator.trySplit();
      return (fromSplit == null) ? null : filter(fromSplit, predicate);
    }

    @Override
    public long estimateSize() {
      return fromSpliterator.estimateSize() / 2;
    }

    @Override
    public Comparator<? super T> getComparator() {
      return fromSpliterator.getComparator();
    }

    @Override
    public int characteristics() {
      return fromSpliterator.characteristics()
          & (Spliterator.DISTINCT
              | Spliterator.NONNULL
              | Spliterator.ORDERED
              | Spliterator.SORTED);
    }
  }
  return new Splitr();
}
 
开发者ID:zugzug90,项目名称:guava-mock,代码行数:55,代码来源:CollectSpliterators.java

示例12: forEachWithCancel

import java.util.Spliterator; //导入方法依赖的package包/类
@Override
final void forEachWithCancel(Spliterator<P_OUT> spliterator, Sink<P_OUT> sink) {
    do { } while (!sink.cancellationRequested() && spliterator.tryAdvance(sink));
}
 
开发者ID:SunburstApps,项目名称:OpenJSharp,代码行数:5,代码来源:ReferencePipeline.java

示例13: truncate

import java.util.Spliterator; //导入方法依赖的package包/类
/**
 * Return a node describing a subsequence of the elements of this node,
 * starting at the given inclusive start offset and ending at the given
 * exclusive end offset.
 *
 * @param from The (inclusive) starting offset of elements to include, must
 *             be in range 0..count().
 * @param to The (exclusive) end offset of elements to include, must be
 *           in range 0..count().
 * @param generator A function to be used to create a new array, if needed,
 *                  for reference nodes.
 * @return the truncated node
 */
default Node<T> truncate(long from, long to, IntFunction<T[]> generator) {
    if (from == 0 && to == count())
        return this;
    Spliterator<T> spliterator = spliterator();
    long size = to - from;
    Node.Builder<T> nodeBuilder = Nodes.builder(size, generator);
    nodeBuilder.begin(size);
    for (int i = 0; i < from && spliterator.tryAdvance(e -> { }); i++) { }
    for (int i = 0; (i < size) && spliterator.tryAdvance(nodeBuilder); i++) { }
    nodeBuilder.end();
    return nodeBuilder.build();
}
 
开发者ID:SunburstApps,项目名称:OpenJSharp,代码行数:26,代码来源:Node.java


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