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