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


Java Spliterator.characteristics方法代碼示例

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


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

示例1: concat

import java.util.Spliterator; //導入方法依賴的package包/類
/**
 * Returns a {@link Stream} containing the elements of the first stream, followed by the elements
 * of the second stream, and so on.
 *
 * <p>This is equivalent to {@code Stream.of(streams).flatMap(stream -> stream)}, but the returned
 * stream may perform better.
 *
 * @see Stream#concat(Stream, Stream)
 */
@SafeVarargs
public static <T> Stream<T> concat(Stream<? extends T>... streams) {
  // TODO(lowasser): consider an implementation that can support SUBSIZED
  boolean isParallel = false;
  int characteristics = Spliterator.ORDERED | Spliterator.SIZED | Spliterator.NONNULL;
  long estimatedSize = 0L;
  ImmutableList.Builder<Spliterator<? extends T>> splitrsBuilder =
      new ImmutableList.Builder<>(streams.length);
  for (Stream<? extends T> stream : streams) {
    isParallel |= stream.isParallel();
    Spliterator<? extends T> splitr = stream.spliterator();
    splitrsBuilder.add(splitr);
    characteristics &= splitr.characteristics();
    estimatedSize = LongMath.saturatedAdd(estimatedSize, splitr.estimateSize());
  }
  return StreamSupport.stream(
      CollectSpliterators.flatMap(
          splitrsBuilder.build().spliterator(),
          splitr -> (Spliterator<T>) splitr,
          characteristics,
          estimatedSize),
      isParallel);
}
 
開發者ID:zugzug90,項目名稱:guava-mock,代碼行數:33,代碼來源:Streams.java

示例2: testSpliterator_characteristics

import java.util.Spliterator; //導入方法依賴的package包/類
/**
 * Spliterator characteristics are as advertised
 */
public void testSpliterator_characteristics() {
    ArrayDeque q = new ArrayDeque();
    Spliterator s = q.spliterator();
    int characteristics = s.characteristics();
    int required = Spliterator.NONNULL
        | Spliterator.ORDERED
        | Spliterator.SIZED
        | Spliterator.SUBSIZED;
    assertEquals(required, characteristics & required);
    assertTrue(s.hasCharacteristics(required));
    assertEquals(0, characteristics
                 & (Spliterator.CONCURRENT
                    | Spliterator.DISTINCT
                    | Spliterator.IMMUTABLE
                    | Spliterator.SORTED));
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:20,代碼來源:ArrayDeque8Test.java

示例3: zip

import java.util.Spliterator; //導入方法依賴的package包/類
/**
 * Returns a stream in which each element is the result of passing the corresponding elementY of
 * each of {@code streamA} and {@code streamB} to {@code function}.
 *
 * <p>For example:
 *
 * <pre>{@code
 * Streams.zip(
 *   Stream.of("foo1", "foo2", "foo3"),
 *   Stream.of("bar1", "bar2"),
 *   (arg1, arg2) -> arg1 + ":" + arg2)
 * }</pre>
 *
 * <p>will return {@code Stream.of("foo1:bar1", "foo2:bar2")}.
 *
 * <p>The resulting stream will only be as long as the shorter of the two input streams; if one
 * stream is longer, its extra elements will be ignored.
 *
 * <p>Note that if you are calling {@link Stream#forEach} on the resulting stream, you might want
 * to consider using {@link #forEachPair} instead of this method.
 *
 * <p><b>Performance note:</b> The resulting stream is not <a
 * href="http://gee.cs.oswego.edu/dl/html/StreamParallelGuidance.html">efficiently splittable</a>.
 * This may harm parallel performance.
 */
public static <A, B, R> Stream<R> zip(
    Stream<A> streamA, Stream<B> streamB, BiFunction<? super A, ? super B, R> function) {
  checkNotNull(streamA);
  checkNotNull(streamB);
  checkNotNull(function);
  boolean isParallel = streamA.isParallel() || streamB.isParallel(); // same as Stream.concat
  Spliterator<A> splitrA = streamA.spliterator();
  Spliterator<B> splitrB = streamB.spliterator();
  int characteristics =
      splitrA.characteristics()
          & splitrB.characteristics()
          & (Spliterator.SIZED | Spliterator.ORDERED);
  Iterator<A> itrA = Spliterators.iterator(splitrA);
  Iterator<B> itrB = Spliterators.iterator(splitrB);
  return StreamSupport.stream(
      new AbstractSpliterator<R>(
          Math.min(splitrA.estimateSize(), splitrB.estimateSize()), characteristics) {
        @Override
        public boolean tryAdvance(Consumer<? super R> action) {
          if (itrA.hasNext() && itrB.hasNext()) {
            action.accept(function.apply(itrA.next(), itrB.next()));
            return true;
          }
          return false;
        }
      },
      isParallel);
}
 
開發者ID:zugzug90,項目名稱:guava-mock,代碼行數:54,代碼來源:Streams.java

示例4: lateBindingTestWithCharacteritics

import java.util.Spliterator; //導入方法依賴的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: testSpliterator_characteristics

import java.util.Spliterator; //導入方法依賴的package包/類
/**
 * Spliterator characteristics are as advertised
 */
public void testSpliterator_characteristics() {
    LinkedBlockingQueue q = new LinkedBlockingQueue();
    Spliterator s = q.spliterator();
    int characteristics = s.characteristics();
    int required = Spliterator.CONCURRENT
        | Spliterator.NONNULL
        | Spliterator.ORDERED;
    assertEquals(required, characteristics & required);
    assertTrue(s.hasCharacteristics(required));
    assertEquals(0, characteristics
                 & (Spliterator.DISTINCT
                    | Spliterator.IMMUTABLE
                    | Spliterator.SORTED));
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:18,代碼來源:LinkedBlockingQueue8Test.java

示例6: testSpliterator_characteristics

import java.util.Spliterator; //導入方法依賴的package包/類
/**
 * Spliterator characteristics are as advertised
 */
public void testSpliterator_characteristics() {
    LinkedBlockingDeque q = new LinkedBlockingDeque();
    Spliterator s = q.spliterator();
    int characteristics = s.characteristics();
    int required = Spliterator.CONCURRENT
        | Spliterator.NONNULL
        | Spliterator.ORDERED;
    assertEquals(required, characteristics & required);
    assertTrue(s.hasCharacteristics(required));
    assertEquals(0, characteristics
                 & (Spliterator.DISTINCT
                    | Spliterator.IMMUTABLE
                    | Spliterator.SORTED));
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:18,代碼來源:LinkedBlockingDeque8Test.java

示例7: testCharacteristics

import java.util.Spliterator; //導入方法依賴的package包/類
@Test(dataProvider = "Source.Non.Binding.Characteristics")
public <T> void testCharacteristics(String description, Supplier<Source<T>> ss) {
    Source<T> source = ss.get();
    Spliterator<T> s = source.spliterator();

    s.characteristics();
    source.update();

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

    Set<T> e = new HashSet<>();
    source.spliterator().forEachRemaining(e::add);
    assertEquals(a, e);
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:16,代碼來源:SpliteratorLateBindingTest.java

示例8: zip

import java.util.Spliterator; //導入方法依賴的package包/類
/**
 * Returns a stream in which each element is the result of passing the corresponding element of
 * each of {@code streamA} and {@code streamB} to {@code function}.
 *
 * <p>For example:
 *
 * <pre>{@code
 * Streams.zip(
 *   Stream.of("foo1", "foo2", "foo3"),
 *   Stream.of("bar1", "bar2"),
 *   (arg1, arg2) -> arg1 + ":" + arg2)
 * }</pre>
 *
 * <p>will return {@code Stream.of("foo1:bar1", "foo2:bar2")}.
 *
 * <p>The resulting stream will only be as long as the shorter of the two input streams; if one
 * stream is longer, its extra elements will be ignored.
 *
 * <p>The resulting stream is not <a
 * href="http://gee.cs.oswego.edu/dl/html/StreamParallelGuidance.html">efficiently splittable</a>.
 * This may harm parallel performance.
 */
public static <A, B, R> Stream<R> zip(
    Stream<A> streamA, Stream<B> streamB, BiFunction<? super A, ? super B, R> function) {
  checkNotNull(streamA);
  checkNotNull(streamB);
  checkNotNull(function);
  boolean isParallel = streamA.isParallel() || streamB.isParallel(); // same as Stream.concat
  Spliterator<A> splitrA = streamA.spliterator();
  Spliterator<B> splitrB = streamB.spliterator();
  int characteristics =
      splitrA.characteristics()
          & splitrB.characteristics()
          & (Spliterator.SIZED | Spliterator.ORDERED);
  Iterator<A> itrA = Spliterators.iterator(splitrA);
  Iterator<B> itrB = Spliterators.iterator(splitrB);
  return StreamSupport.stream(
      new AbstractSpliterator<R>(
          Math.min(splitrA.estimateSize(), splitrB.estimateSize()), characteristics) {
        @Override
        public boolean tryAdvance(Consumer<? super R> action) {
          if (itrA.hasNext() && itrB.hasNext()) {
            action.accept(function.apply(itrA.next(), itrB.next()));
            return true;
          }
          return false;
        }
      },
      isParallel);
}
 
開發者ID:paul-hammant,項目名稱:googles-monorepo-demo,代碼行數:51,代碼來源:Streams.java

示例9: 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

示例10: BatchingSpliterator

import java.util.Spliterator; //導入方法依賴的package包/類
private BatchingSpliterator(Spliterator<T> original, int batchSize){
	super(original.estimateSize() / batchSize, original.characteristics());
	this.original = original;
	this.batchSize = batchSize;
}
 
開發者ID:hotpads,項目名稱:datarouter,代碼行數:6,代碼來源:StreamTool.java

示例11: SplitNode

import java.util.Spliterator; //導入方法依賴的package包/類
SplitNode(Consumer<T> c, Spliterator<T> s) {
    this(c, s.characteristics(), s);
}
 
開發者ID:lambdalab-mirror,項目名稱:jdk8u-jdk,代碼行數:4,代碼來源:SpliteratorTestHelper.java

示例12: fromCharacteristics

import java.util.Spliterator; //導入方法依賴的package包/類
/**
 * Converts a spliterator characteristic bit set to stream flags.
 *
 * @implSpec
 * If the spliterator is naturally {@code SORTED} (the associated
 * {@code Comparator} is {@code null}) then the characteristic is converted
 * to the {@link #SORTED} flag, otherwise the characteristic is not
 * converted.
 *
 * @param spliterator the spliterator from which to obtain characteristic
 *        bit set.
 * @return the stream flags.
 */
static int fromCharacteristics(Spliterator<?> spliterator) {
    int characteristics = spliterator.characteristics();
    if ((characteristics & Spliterator.SORTED) != 0 && spliterator.getComparator() != null) {
        // Do not propagate the SORTED characteristic if it does not correspond
        // to a natural sort order
        return characteristics & SPLITERATOR_CHARACTERISTICS_MASK & ~Spliterator.SORTED;
    }
    else {
        return characteristics & SPLITERATOR_CHARACTERISTICS_MASK;
    }
}
 
開發者ID:SunburstApps,項目名稱:OpenJSharp,代碼行數:25,代碼來源:StreamOpFlag.java


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