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


Java Spliterators类代码示例

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


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

示例1: testSpliteratorAIOBEsFromSpliterators

import java.util.Spliterators; //导入依赖的package包/类
@Test
public void testSpliteratorAIOBEsFromSpliterators() {
    // origin > fence
    assertThrowsAIOOB(() -> Spliterators.spliterator(new int[]{}, 1, 0, 0));
    assertThrowsAIOOB(() -> Spliterators.spliterator(new long[]{}, 1, 0, 0));
    assertThrowsAIOOB(() -> Spliterators.spliterator(new double[]{}, 1, 0, 0));
    assertThrowsAIOOB(() -> Spliterators.spliterator(new String[]{}, 1, 0, 0));

    // bad origin
    assertThrowsAIOOB(() -> Spliterators.spliterator(new int[]{}, -1, 0, 0));
    assertThrowsAIOOB(() -> Spliterators.spliterator(new long[]{}, -1, 0, 0));
    assertThrowsAIOOB(() -> Spliterators.spliterator(new double[]{}, -1, 0, 0));
    assertThrowsAIOOB(() -> Spliterators.spliterator(new String[]{}, -1, 0, 0));

    // bad fence
    assertThrowsAIOOB(() -> Spliterators.spliterator(new int[]{}, 0, 1, 0));
    assertThrowsAIOOB(() -> Spliterators.spliterator(new long[]{}, 0, 1, 0));
    assertThrowsAIOOB(() -> Spliterators.spliterator(new double[]{}, 0, 1, 0));
    assertThrowsAIOOB(() -> Spliterators.spliterator(new String[]{}, 0, 1, 0));
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:21,代码来源:StreamAndSpliterator.java

示例2: trySplit

import java.util.Spliterators; //导入依赖的package包/类
public Spliterator<E> trySplit() {
    Node<E> p, q;
    if ((p = current()) == null || (q = p.next) == null)
        return null;
    int i = 0, n = batch = Math.min(batch + 1, MAX_BATCH);
    Object[] a = null;
    do {
        final E e;
        if ((e = p.item) != null) {
            if (a == null)
                a = new Object[n];
            a[i++] = e;
        }
        if (p == (p = q))
            p = first();
    } while (p != null && (q = p.next) != null && i < n);
    setCurrent(p);
    return (i == 0) ? null :
        Spliterators.spliterator(a, 0, i, (Spliterator.ORDERED |
                                           Spliterator.NONNULL |
                                           Spliterator.CONCURRENT));
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:23,代码来源:ConcurrentLinkedDeque.java

示例3: iterate

import java.util.Spliterators; //导入依赖的package包/类
/**
 * Returns an infinite sequential ordered {@code Stream} produced by iterative
 * application of a function {@code f} to an initial element {@code seed},
 * producing a {@code Stream} consisting of {@code seed}, {@code f(seed)},
 * {@code f(f(seed))}, etc.
 *
 * <p>The first element (position {@code 0}) in the {@code Stream} will be
 * the provided {@code seed}.  For {@code n > 0}, the element at position
 * {@code n}, will be the result of applying the function {@code f} to the
 * element at position {@code n - 1}.
 *
 * @param <T> the type of stream elements
 * @param seed the initial element
 * @param f a function to be applied to to the previous element to produce
 *          a new element
 * @return a new sequential {@code Stream}
 */
public static<T> Stream<T> iterate(final T seed, final UnaryOperator<T> f) {
    Objects.requireNonNull(f);
    final Iterator<T> iterator = new Iterator<T>() {
        @SuppressWarnings("unchecked")
        T t = (T) Streams.NONE;

        @Override
        public boolean hasNext() {
            return true;
        }

        @Override
        public T next() {
            return t = (t == Streams.NONE) ? seed : f.apply(t);
        }
    };
    return StreamSupport.stream(Spliterators.spliteratorUnknownSize(
            iterator,
            Spliterator.ORDERED | Spliterator.IMMUTABLE), false);
}
 
开发者ID:SunburstApps,项目名称:OpenJSharp,代码行数:38,代码来源:Stream.java

示例4: iterate

import java.util.Spliterators; //导入依赖的package包/类
/**
 * Returns an infinite sequential ordered {@code LongStream} produced by iterative
 * application of a function {@code f} to an initial element {@code seed},
 * producing a {@code Stream} consisting of {@code seed}, {@code f(seed)},
 * {@code f(f(seed))}, etc.
 *
 * <p>The first element (position {@code 0}) in the {@code LongStream} will
 * be the provided {@code seed}.  For {@code n > 0}, the element at position
 * {@code n}, will be the result of applying the function {@code f} to the
 * element at position {@code n - 1}.
 *
 * @param seed the initial element
 * @param f a function to be applied to to the previous element to produce
 *          a new element
 * @return a new sequential {@code LongStream}
 */
public static LongStream iterate(final long seed, final LongUnaryOperator f) {
    Objects.requireNonNull(f);
    final PrimitiveIterator.OfLong iterator = new PrimitiveIterator.OfLong() {
        long t = seed;

        @Override
        public boolean hasNext() {
            return true;
        }

        @Override
        public long nextLong() {
            long v = t;
            t = f.applyAsLong(t);
            return v;
        }
    };
    return StreamSupport.longStream(Spliterators.spliteratorUnknownSize(
            iterator,
            Spliterator.ORDERED | Spliterator.IMMUTABLE | Spliterator.NONNULL), false);
}
 
开发者ID:SunburstApps,项目名称:OpenJSharp,代码行数:38,代码来源:LongStream.java

示例5: iterate

import java.util.Spliterators; //导入依赖的package包/类
/**
 * Returns an infinite sequential ordered {@code DoubleStream} produced by iterative
 * application of a function {@code f} to an initial element {@code seed},
 * producing a {@code Stream} consisting of {@code seed}, {@code f(seed)},
 * {@code f(f(seed))}, etc.
 *
 * <p>The first element (position {@code 0}) in the {@code DoubleStream}
 * will be the provided {@code seed}.  For {@code n > 0}, the element at
 * position {@code n}, will be the result of applying the function {@code f}
 *  to the element at position {@code n - 1}.
 *
 * @param seed the initial element
 * @param f a function to be applied to to the previous element to produce
 *          a new element
 * @return a new sequential {@code DoubleStream}
 */
public static DoubleStream iterate(final double seed, final DoubleUnaryOperator f) {
    Objects.requireNonNull(f);
    final PrimitiveIterator.OfDouble iterator = new PrimitiveIterator.OfDouble() {
        double t = seed;

        @Override
        public boolean hasNext() {
            return true;
        }

        @Override
        public double nextDouble() {
            double v = t;
            t = f.applyAsDouble(t);
            return v;
        }
    };
    return StreamSupport.doubleStream(Spliterators.spliteratorUnknownSize(
            iterator,
            Spliterator.ORDERED | Spliterator.IMMUTABLE | Spliterator.NONNULL), false);
}
 
开发者ID:SunburstApps,项目名称:OpenJSharp,代码行数:38,代码来源:DoubleStream.java

示例6: iterate

import java.util.Spliterators; //导入依赖的package包/类
/**
 * Returns an infinite sequential ordered {@code LongStream} produced by iterative
 * application of a function {@code f} to an initial element {@code seed},
 * producing a {@code Stream} consisting of {@code seed}, {@code f(seed)},
 * {@code f(f(seed))}, etc.
 *
 * <p>The first element (position {@code 0}) in the {@code LongStream} will
 * be the provided {@code seed}.  For {@code n > 0}, the element at position
 * {@code n}, will be the result of applying the function {@code f} to the
 * element at position {@code n - 1}.
 *
 * <p>The action of applying {@code f} for one element
 * <a href="../concurrent/package-summary.html#MemoryVisibility"><i>happens-before</i></a>
 * the action of applying {@code f} for subsequent elements.  For any given
 * element the action may be performed in whatever thread the library
 * chooses.
 *
 * @param seed the initial element
 * @param f a function to be applied to the previous element to produce
 *          a new element
 * @return a new sequential {@code LongStream}
 */
public static LongStream iterate(final long seed, final LongUnaryOperator f) {
    Objects.requireNonNull(f);
    Spliterator.OfLong spliterator = new Spliterators.AbstractLongSpliterator(Long.MAX_VALUE,
           Spliterator.ORDERED | Spliterator.IMMUTABLE | Spliterator.NONNULL) {
        long prev;
        boolean started;

        @Override
        public boolean tryAdvance(LongConsumer action) {
            Objects.requireNonNull(action);
            long t;
            if (started)
                t = f.applyAsLong(prev);
            else {
                t = seed;
                started = true;
            }
            action.accept(prev = t);
            return true;
        }
    };
    return StreamSupport.longStream(spliterator, false);
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:46,代码来源:LongStream.java

示例7: takeIntWhile

import java.util.Spliterators; //导入依赖的package包/类
/**
 * Adapted from StackOverflow {@linkplain so http://stackoverflow.com/questions/20746429/limit-a-stream-by-a-predicate}
 *
 * @param splitr the original Spliterator
 * @param predicate the predicate
 * @return a Spliterator.OfInt
 */
private static Spliterator.OfInt takeIntWhile(Spliterator.OfInt splitr, IntPredicate predicate) {
    return new Spliterators.AbstractIntSpliterator(splitr.estimateSize(), 0) {
        boolean stillGoing = true;

        @Override
        public boolean tryAdvance(IntConsumer consumer) {
            if (stillGoing) {
                boolean hadNext = splitr.tryAdvance((int elem) -> {
                    if (predicate.test(elem)) {
                        consumer.accept(elem);
                    } else {
                        stillGoing = false;
                    }
                });
                return hadNext && stillGoing;
            }
            return false;
        }
    };
}
 
开发者ID:fralalonde,项目名称:iostream,代码行数:28,代码来源:StreamInputAdapter.java

示例8: iterate

import java.util.Spliterators; //导入依赖的package包/类
/**
 * Returns an infinite sequential ordered {@code IntStream} produced by iterative
 * application of a function {@code f} to an initial element {@code seed},
 * producing a {@code Stream} consisting of {@code seed}, {@code f(seed)},
 * {@code f(f(seed))}, etc.
 *
 * <p>The first element (position {@code 0}) in the {@code IntStream} will be
 * the provided {@code seed}.  For {@code n > 0}, the element at position
 * {@code n}, will be the result of applying the function {@code f} to the
 * element at position {@code n - 1}.
 *
 * @param seed the initial element
 * @param f a function to be applied to to the previous element to produce
 *          a new element
 * @return A new sequential {@code IntStream}
 */
public static IntStream iterate(final int seed, final IntUnaryOperator f) {
    Objects.requireNonNull(f);
    final PrimitiveIterator.OfInt iterator = new PrimitiveIterator.OfInt() {
        int t = seed;

        @Override
        public boolean hasNext() {
            return true;
        }

        @Override
        public int nextInt() {
            int v = t;
            t = f.applyAsInt(t);
            return v;
        }
    };
    return StreamSupport.intStream(Spliterators.spliteratorUnknownSize(
            iterator,
            Spliterator.ORDERED | Spliterator.IMMUTABLE | Spliterator.NONNULL), false);
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:38,代码来源:IntStream.java

示例9: lines

import java.util.Spliterators; //导入依赖的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>() {
        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:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:61,代码来源:BufferedReader.java

示例10: enumerationAsStream

import java.util.Spliterators; //导入依赖的package包/类
public static <T> Stream<T> enumerationAsStream(Enumeration<T> e) {
    return StreamSupport.stream(
            Spliterators.spliteratorUnknownSize(
                    new Iterator<T>() {
                public T next() {
                    return e.nextElement();
                }

                public boolean hasNext() {
                    return e.hasMoreElements();
                }
            }, Spliterator.ORDERED), false);
}
 
开发者ID:Adobe-Consulting-Services,项目名称:aem-epic-tool,代码行数:14,代码来源:DataUtil.java

示例11: testSpliteratorNPEsFromSpliterators

import java.util.Spliterators; //导入依赖的package包/类
@Test
public void testSpliteratorNPEsFromSpliterators() {
    assertThrowsNPE(() -> Spliterators.spliterator((int[]) null, 0, 0, 0));
    assertThrowsNPE(() -> Spliterators.spliterator((long[]) null, 0, 0, 0));
    assertThrowsNPE(() -> Spliterators.spliterator((double[]) null, 0, 0, 0));
    assertThrowsNPE(() -> Spliterators.spliterator((String[]) null, 0, 0, 0));
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:8,代码来源:StreamAndSpliterator.java

示例12: zip

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

示例13: entrySpliterator

import java.util.Spliterators; //导入依赖的package包/类
@Override
Spliterator<Entry<C, V>> entrySpliterator() {
  Map<C, V> map = backingRowMap();
  if (map == null) {
    return Spliterators.emptySpliterator();
  }
  return CollectSpliterators.map(map.entrySet().spliterator(), this::wrapEntry);
}
 
开发者ID:zugzug90,项目名称:guava-mock,代码行数:9,代码来源:StandardTable.java

示例14: lines

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

示例15: stream

import java.util.Spliterators; //导入依赖的package包/类
/**
 * Return a stream with the data lines.
 *
 * @return a stream with the data lines
 */
public Stream<String[]> stream() {
	final DataIterator iterator = new DataIterator(getResourcePath());
	final Spliterator<String[]> spliterator = Spliterators
		.spliteratorUnknownSize(iterator, 0);

	return StreamSupport
		.stream(spliterator, false)
		.onClose(iterator::close);
}
 
开发者ID:jenetics,项目名称:prngine,代码行数:15,代码来源:TestData.java


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