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


Java Collection.spliterator方法代码示例

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


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

示例1: testStickySpliteratorExhaustion

import java.util.Collection; //导入方法依赖的package包/类
/**
 * Concurrent Spliterators, once exhausted, stay exhausted.
 */
public void testStickySpliteratorExhaustion() throws Throwable {
    if (!impl.isConcurrent()) return;
    if (!testImplementationDetails) return;
    final ThreadLocalRandom rnd = ThreadLocalRandom.current();
    final Consumer alwaysThrows = e -> { throw new AssertionError(); };
    final Collection c = impl.emptyCollection();
    final Spliterator s = c.spliterator();
    if (rnd.nextBoolean()) {
        assertFalse(s.tryAdvance(alwaysThrows));
    } else {
        s.forEachRemaining(alwaysThrows);
    }
    final Object one = impl.makeElement(1);
    // Spliterator should not notice added element
    c.add(one);
    if (rnd.nextBoolean()) {
        assertFalse(s.tryAdvance(alwaysThrows));
    } else {
        s.forEachRemaining(alwaysThrows);
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:25,代码来源:Collection8Test.java

示例2: lateBindingTestWithForEach

import java.util.Collection; //导入方法依赖的package包/类
@Test(dataProvider = "Source")
public <T> void lateBindingTestWithForEach(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<>();
    s.forEachRemaining(r::add);

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

示例3: lateBindingTestWithTryAdvance

import java.util.Collection; //导入方法依赖的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

示例4: lateBindingTestWithCharacteritics

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

    Set<T> r = new HashSet<>();
    s.forEachRemaining(r::add);

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

示例5: testFailFastTestWithForEach

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

    executeAndCatch(() -> s.forEachRemaining(e -> {
        source.update();
    }));
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:11,代码来源:SpliteratorLateBindingFailFastTest.java

示例6: testElementRemovalDuringTraversal

import java.util.Collection; //导入方法依赖的package包/类
/**
 * All elements removed in the middle of CONCURRENT traversal.
 */
public void testElementRemovalDuringTraversal() {
    Collection c = impl.emptyCollection();
    ThreadLocalRandom rnd = ThreadLocalRandom.current();
    int n = rnd.nextInt(6);
    ArrayList copy = new ArrayList();
    for (int i = 0; i < n; i++) {
        Object x = impl.makeElement(i);
        copy.add(x);
        c.add(x);
    }
    ArrayList iterated = new ArrayList();
    ArrayList spliterated = new ArrayList();
    Spliterator s = c.spliterator();
    Iterator it = c.iterator();
    for (int i = rnd.nextInt(n + 1); --i >= 0; ) {
        assertTrue(s.tryAdvance(spliterated::add));
        if (rnd.nextBoolean()) assertTrue(it.hasNext());
        iterated.add(it.next());
    }
    Consumer alwaysThrows = e -> { throw new AssertionError(); };
    if (s.hasCharacteristics(Spliterator.CONCURRENT)) {
        c.clear();          // TODO: many more removal methods
        if (testImplementationDetails
            && !(c instanceof java.util.concurrent.ArrayBlockingQueue)) {
            if (rnd.nextBoolean())
                assertFalse(s.tryAdvance(alwaysThrows));
            else
                s.forEachRemaining(alwaysThrows);
        }
        if (it.hasNext()) iterated.add(it.next());
        if (rnd.nextBoolean()) assertIteratorExhausted(it);
    }
    assertTrue(copy.containsAll(iterated));
    assertTrue(copy.containsAll(spliterated));
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:39,代码来源:Collection8Test.java

示例7: testGetComparator_IllegalStateException

import java.util.Collection; //导入方法依赖的package包/类
/**
 * Spliterator.getComparator throws IllegalStateException iff the
 * spliterator does not report SORTED.
 */
public void testGetComparator_IllegalStateException() {
    Collection c = impl.emptyCollection();
    Spliterator s = c.spliterator();
    boolean reportsSorted = s.hasCharacteristics(Spliterator.SORTED);
    try {
        s.getComparator();
        assertTrue(reportsSorted);
    } catch (IllegalStateException ex) {
        assertFalse(reportsSorted);
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:16,代码来源:Collection8Test.java

示例8: testTraversalEquivalence

import java.util.Collection; //导入方法依赖的package包/类
/**
 * Various ways of traversing a collection yield same elements
 */
public void testTraversalEquivalence() {
    Collection c = impl.emptyCollection();
    ThreadLocalRandom rnd = ThreadLocalRandom.current();
    int n = rnd.nextInt(6);
    for (int i = 0; i < n; i++) c.add(impl.makeElement(i));
    ArrayList iterated = new ArrayList();
    ArrayList iteratedForEachRemaining = new ArrayList();
    ArrayList tryAdvanced = new ArrayList();
    ArrayList spliterated = new ArrayList();
    ArrayList splitonced = new ArrayList();
    ArrayList forEached = new ArrayList();
    ArrayList streamForEached = new ArrayList();
    ConcurrentLinkedQueue parallelStreamForEached = new ConcurrentLinkedQueue();
    ArrayList removeIfed = new ArrayList();
    for (Object x : c) iterated.add(x);
    c.iterator().forEachRemaining(iteratedForEachRemaining::add);
    for (Spliterator s = c.spliterator();
         s.tryAdvance(tryAdvanced::add); ) {}
    c.spliterator().forEachRemaining(spliterated::add);
    {                       // trySplit returns "strict prefix"
        Spliterator s1 = c.spliterator(), s2 = s1.trySplit();
        if (s2 != null) s2.forEachRemaining(splitonced::add);
        s1.forEachRemaining(splitonced::add);
    }
    c.forEach(forEached::add);
    c.stream().forEach(streamForEached::add);
    c.parallelStream().forEach(parallelStreamForEached::add);
    c.removeIf(e -> { removeIfed.add(e); return false; });
    boolean ordered =
        c.spliterator().hasCharacteristics(Spliterator.ORDERED);
    if (c instanceof List || c instanceof Deque)
        assertTrue(ordered);
    HashSet cset = new HashSet(c);
    assertEquals(cset, new HashSet(parallelStreamForEached));
    if (ordered) {
        assertEquals(iterated, iteratedForEachRemaining);
        assertEquals(iterated, tryAdvanced);
        assertEquals(iterated, spliterated);
        assertEquals(iterated, splitonced);
        assertEquals(iterated, forEached);
        assertEquals(iterated, streamForEached);
        assertEquals(iterated, removeIfed);
    } else {
        assertEquals(cset, new HashSet(iterated));
        assertEquals(cset, new HashSet(iteratedForEachRemaining));
        assertEquals(cset, new HashSet(tryAdvanced));
        assertEquals(cset, new HashSet(spliterated));
        assertEquals(cset, new HashSet(splitonced));
        assertEquals(cset, new HashSet(forEached));
        assertEquals(cset, new HashSet(streamForEached));
        assertEquals(cset, new HashSet(removeIfed));
    }
    if (c instanceof Deque) {
        Deque d = (Deque) c;
        ArrayList descending = new ArrayList();
        ArrayList descendingForEachRemaining = new ArrayList();
        for (Iterator it = d.descendingIterator(); it.hasNext(); )
            descending.add(it.next());
        d.descendingIterator().forEachRemaining(
            e -> descendingForEachRemaining.add(e));
        Collections.reverse(descending);
        Collections.reverse(descendingForEachRemaining);
        assertEquals(iterated, descending);
        assertEquals(iterated, descendingForEachRemaining);
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:70,代码来源:Collection8Test.java


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