當前位置: 首頁>>代碼示例>>Java>>正文


Java Spliterator類代碼示例

本文整理匯總了Java中java.util.Spliterator的典型用法代碼示例。如果您正苦於以下問題:Java Spliterator類的具體用法?Java Spliterator怎麽用?Java Spliterator使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


Spliterator類屬於java.util包,在下文中一共展示了Spliterator類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: opEvaluateParallel

import java.util.Spliterator; //導入依賴的package包/類
@Override
public <P_IN> Node<Integer> opEvaluateParallel(PipelineHelper<Integer> helper,
                                               Spliterator<P_IN> spliterator,
                                               IntFunction<Integer[]> generator) {
    if (StreamOpFlag.SORTED.isKnown(helper.getStreamAndOpFlags())) {
        return helper.evaluate(spliterator, false, generator);
    }
    else {
        Node.OfInt n = (Node.OfInt) helper.evaluate(spliterator, true, generator);

        int[] content = n.asPrimitiveArray();
        Arrays.parallelSort(content);

        return Nodes.node(content);
    }
}
 
開發者ID:SunburstApps,項目名稱:OpenJSharp,代碼行數:17,代碼來源:SortedOps.java

示例2: trySplit

import java.util.Spliterator; //導入依賴的package包/類
@Override
public Spliterator<E> trySplit() {
  Node<E> p;
  int s = getEst();
  if (s > 1 && (p = current) != null) {
    int n = batch + BATCH_UNIT;
    if (n > s) {
      n = s;
    }
    if (n > MAX_BATCH) {
      n = MAX_BATCH;
    }
    Object[] a = new Object[n];
    int j = 0;
    do {
      a[j++] = p.item;
    } while ((p = p.next) != null && j < n);
    current = p;
    batch = j;
    est = s - j;
    return Spliterators.spliterator(a, 0, j, Spliterator.ORDERED);
  }
  return null;
}
 
開發者ID:XDean,項目名稱:Java-EX,代碼行數:25,代碼來源:LinkedList.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: shouldSplitPartOfTask

import java.util.Spliterator; //導入依賴的package包/類
@Test
public void shouldSplitPartOfTask() {
    List<String> lines = asList("one", "two");

    TextSpliterator ts = new TextSpliterator(lines);

    Spliterator<String> fork = ts.trySplit();
    assertNotNull(fork);

    StringBuilder sb = new StringBuilder();
    assertTrue(ts.tryAdvance(sb::append));
    assertEquals("one", sb.toString());
    assertFalse(ts.tryAdvance(sb::append));
    assertEquals("one", sb.toString());

    sb = new StringBuilder();
    assertTrue(fork.tryAdvance(sb::append));
    assertEquals("two", sb.toString());
    assertFalse(fork.tryAdvance(sb::append));
    assertEquals("two", sb.toString());
}
 
開發者ID:trejnado,項目名稱:java8-practice,代碼行數:22,代碼來源:TextSpliteratorTest.java

示例5: testSplitOnce

import java.util.Spliterator; //導入依賴的package包/類
private static <T, S extends Spliterator<T>> void testSplitOnce(
        Collection<T> exp,
        Supplier<S> supplier,
        UnaryOperator<Consumer<T>> boxingAdapter) {
    S spliterator = supplier.get();
    long sizeIfKnown = spliterator.getExactSizeIfKnown();
    boolean isOrdered = spliterator.hasCharacteristics(Spliterator.ORDERED);

    ArrayList<T> fromSplit = new ArrayList<>();
    Spliterator<T> s1 = supplier.get();
    Spliterator<T> s2 = s1.trySplit();
    long s1Size = s1.getExactSizeIfKnown();
    long s2Size = (s2 != null) ? s2.getExactSizeIfKnown() : 0;

    Consumer<T> addToFromSplit = boxingAdapter.apply(fromSplit::add);
    if (s2 != null)
        s2.forEachRemaining(addToFromSplit);
    s1.forEachRemaining(addToFromSplit);

    if (sizeIfKnown >= 0) {
        assertEquals(sizeIfKnown, fromSplit.size());
        if (s1Size >= 0 && s2Size >= 0)
            assertEquals(sizeIfKnown, s1Size + s2Size);
    }
    assertContents(fromSplit, exp, isOrdered);
}
 
開發者ID:lambdalab-mirror,項目名稱:jdk8u-jdk,代碼行數:27,代碼來源:SpliteratorCollisions.java

示例6: resources

import java.util.Spliterator; //導入依賴的package包/類
@Override
public Stream<URL> resources(String name) {
    Objects.requireNonNull(name);
    // ordering not specified
    int characteristics = (Spliterator.NONNULL | Spliterator.IMMUTABLE |
                           Spliterator.SIZED | Spliterator.SUBSIZED);
    Supplier<Spliterator<URL>> supplier = () -> {
        try {
            List<URL> urls = findResourcesAsList(name);
            return Spliterators.spliterator(urls, characteristics);
        } catch (IOException e) {
            throw new UncheckedIOException(e);
        }
    };
    Stream<URL> s1 = StreamSupport.stream(supplier, characteristics, false);
    Stream<URL> s2 = parent.resources(name);
    return Stream.concat(s1, s2);
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:19,代碼來源:Loader.java

示例7: entrySet

import java.util.Spliterator; //導入依賴的package包/類
@Override
public Set<Entry<K, V>> entrySet() {
  return new EntrySet<K, V>() {
    @Override
    Map<K, V> map() {
      return IteratorBasedAbstractMap.this;
    }

    @Override
    public Iterator<Entry<K, V>> iterator() {
      return entryIterator();
    }

    @Override
    public Spliterator<Entry<K, V>> spliterator() {
      return entrySpliterator();
    }

    @Override
    public void forEach(Consumer<? super Entry<K, V>> action) {
      forEachEntry(action);
    }
  };
}
 
開發者ID:paul-hammant,項目名稱:googles-monorepo-demo,代碼行數:25,代碼來源:Maps.java

示例8: ForEachTask

import java.util.Spliterator; //導入依賴的package包/類
ForEachTask(PipelineHelper<T> helper,
            Spliterator<S> spliterator,
            Sink<S> sink) {
    super(null);
    this.sink = sink;
    this.helper = helper;
    this.spliterator = spliterator;
    this.targetSize = 0L;
}
 
開發者ID:SunburstApps,項目名稱:OpenJSharp,代碼行數:10,代碼來源:ForEachOps.java

示例9: SliceTask

import java.util.Spliterator; //導入依賴的package包/類
SliceTask(AbstractPipeline<P_OUT, P_OUT, ?> op,
          PipelineHelper<P_OUT> helper,
          Spliterator<P_IN> spliterator,
          IntFunction<P_OUT[]> generator,
          long offset, long size) {
    super(helper, spliterator);
    this.op = op;
    this.generator = generator;
    this.targetOffset = offset;
    this.targetSize = size;
}
 
開發者ID:SunburstApps,項目名稱:OpenJSharp,代碼行數:12,代碼來源:SliceOps.java

示例10: testNullPointerExceptionWithNull

import java.util.Spliterator; //導入依賴的package包/類
@Test(dataProvider = "HashableIntSpliteratorWithNull")
void testNullPointerExceptionWithNull(String description,
                                      Collection<HashableInteger> exp,
                                      Supplier<Spliterator<HashableInteger>> s) {
    assertThrowsNPE(() -> s.get().forEachRemaining(null));
    assertThrowsNPE(() -> s.get().tryAdvance(null));
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:8,代碼來源:SpliteratorCollisions.java

示例11: ofNode

import java.util.Spliterator; //導入依賴的package包/類
public static OfDouble ofNode(String name, Node.OfDouble node) {
    int characteristics = Spliterator.SIZED | Spliterator.ORDERED;
    return new AbstractTestData.DoubleTestData<>(name, node,
                                                 n -> StreamSupport.doubleStream(n::spliterator, characteristics, false),
                                                 n -> StreamSupport.doubleStream(n::spliterator, characteristics, true),
                                                 Node.OfDouble::spliterator,
                                                 n -> (int) n.count());
}
 
開發者ID:lambdalab-mirror,項目名稱:jdk8u-jdk,代碼行數:9,代碼來源:TestData.java

示例12: assertSpliterator

import java.util.Spliterator; //導入依賴的package包/類
private static void assertSpliterator(Spliterator<?> s, int rootCharacteristics) {
    if ((rootCharacteristics & Spliterator.SUBSIZED) != 0) {
        assertTrue(s.hasCharacteristics(Spliterator.SUBSIZED),
                   "Child split is not SUBSIZED when root split is SUBSIZED");
    }
    assertSpliterator(s);
}
 
開發者ID:lambdalab-mirror,項目名稱:jdk8u-jdk,代碼行數:8,代碼來源:SpliteratorTestHelper.java

示例13: trySplit

import java.util.Spliterator; //導入依賴的package包/類
@Override
public Spliterator<P_OUT> trySplit() {
    if (isParallel && !finished) {
        init();

        Spliterator<P_IN> split = spliterator.trySplit();
        return (split == null) ? null : wrap(split);
    }
    else
        return null;
}
 
開發者ID:lambdalab-mirror,項目名稱:jdk8u-jdk,代碼行數:12,代碼來源:StreamSpliterators.java

示例14: compute

import java.util.Spliterator; //導入依賴的package包/類
/**
 * Decides whether or not to split a task further or compute it
 * directly. If computing directly, calls {@code doLeaf} and pass
 * the result to {@code setRawResult}. Otherwise splits off
 * subtasks, forking one and continuing as the other.
 *
 * <p> The method is structured to conserve resources across a
 * range of uses.  The loop continues with one of the child tasks
 * when split, to avoid deep recursion. To cope with spliterators
 * that may be systematically biased toward left-heavy or
 * right-heavy splits, we alternate which child is forked versus
 * continued in the loop.
 */
@Override
public void compute() {
    Spliterator<P_IN> rs = spliterator, ls; // right, left spliterators
    long sizeEstimate = rs.estimateSize();
    long sizeThreshold = getTargetSize(sizeEstimate);
    boolean forkRight = false;
    @SuppressWarnings("unchecked") K task = (K) this;
    while (sizeEstimate > sizeThreshold && (ls = rs.trySplit()) != null) {
        K leftChild, rightChild, taskToFork;
        task.leftChild  = leftChild = task.makeChild(ls);
        task.rightChild = rightChild = task.makeChild(rs);
        task.setPendingCount(1);
        if (forkRight) {
            forkRight = false;
            rs = ls;
            task = leftChild;
            taskToFork = rightChild;
        }
        else {
            forkRight = true;
            task = rightChild;
            taskToFork = leftChild;
        }
        taskToFork.fork();
        sizeEstimate = rs.estimateSize();
    }
    task.setLocalResult(task.doLeaf());
    task.tryComplete();
}
 
開發者ID:SunburstApps,項目名稱:OpenJSharp,代碼行數:43,代碼來源:AbstractTask.java

示例15: lines

import java.util.Spliterator; //導入依賴的package包/類
/**
 * Returns a {@code Stream}, the elements of which are lines read from
 * this {@code BufferedReader}.  The {@link Stream} is lazily populated,
 * i.e., read only occurs during the
 * <a href="../util/stream/package-summary.html#StreamOps">terminal
 * stream operation</a>.
 *
 * <p> The reader must not be operated on during the execution of the
 * terminal stream operation. Otherwise, the result of the terminal stream
 * operation is undefined.
 *
 * <p> After execution of the terminal stream operation there are no
 * guarantees that the reader will be at a specific position from which to
 * read the next character or line.
 *
 * <p> If an {@link IOException} is thrown when accessing the underlying
 * {@code BufferedReader}, it is wrapped in an {@link
 * UncheckedIOException} which will be thrown from the {@code Stream}
 * method that caused the read to take place. This method will return a
 * Stream if invoked on a BufferedReader that is closed. Any operation on
 * that stream that requires reading from the BufferedReader after it is
 * closed, will cause an UncheckedIOException to be thrown.
 *
 * @return a {@code Stream<String>} providing the lines of text
 *         described by this {@code BufferedReader}
 *
 * @since 1.8
 */
public Stream<String> lines() {
    Iterator<String> iter = new Iterator<>() {
        String nextLine = null;

        @Override
        public boolean hasNext() {
            if (nextLine != null) {
                return true;
            } else {
                try {
                    nextLine = readLine();
                    return (nextLine != null);
                } catch (IOException e) {
                    throw new UncheckedIOException(e);
                }
            }
        }

        @Override
        public String next() {
            if (nextLine != null || hasNext()) {
                String line = nextLine;
                nextLine = null;
                return line;
            } else {
                throw new NoSuchElementException();
            }
        }
    };
    return StreamSupport.stream(Spliterators.spliteratorUnknownSize(
            iter, Spliterator.ORDERED | Spliterator.NONNULL), false);
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:61,代碼來源:BufferedReader.java


注:本文中的java.util.Spliterator類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。