Search in sources :

Example 36 with Spliterator

use of java.util.Spliterator in project streamex by amaembo.

the class LongStreamEx method scanLeft.

/**
 * Produces an array containing cumulative results of applying the
 * accumulation function going left to right.
 *
 * <p>
 * This is a terminal operation.
 *
 * <p>
 * For parallel stream it's not guaranteed that accumulator will always be
 * executed in the same thread.
 *
 * <p>
 * This method cannot take all the advantages of parallel streams as it must
 * process elements strictly left to right.
 *
 * @param accumulator a
 *        <a href="package-summary.html#NonInterference">non-interfering
 *        </a>, <a href="package-summary.html#Statelessness">stateless</a>
 *        function for incorporating an additional element into a result
 * @return the array where the first element is the first element of this
 *         stream and every successor element is the result of applying
 *         accumulator function to the previous array element and the
 *         corresponding stream element. The resulting array has the same
 *         length as this stream.
 * @see #foldLeft(LongBinaryOperator)
 * @since 0.5.1
 */
public long[] scanLeft(LongBinaryOperator accumulator) {
    Spliterator.OfLong spliterator = spliterator();
    int size = intSize(spliterator);
    LongBuffer buf = new LongBuffer(size >= 0 ? size : INITIAL_SIZE);
    delegate(spliterator).forEachOrdered(i -> buf.add(buf.size == 0 ? i : accumulator.applyAsLong(buf.data[buf.size - 1], i)));
    return buf.toArray();
}
Also used : LongBuffer(one.util.streamex.Internals.LongBuffer) Spliterator(java.util.Spliterator)

Example 37 with Spliterator

use of java.util.Spliterator in project streamex by amaembo.

the class DoubleStreamEx method toFloatArray.

/**
 * Returns a {@code float[]} array containing the elements of this stream
 * which are converted to floats using {@code (float)} cast operation.
 *
 * <p>
 * This is a terminal operation.
 *
 * @return an array containing the elements of this stream
 * @since 0.3.0
 */
public float[] toFloatArray() {
    if (isParallel())
        return collect(DoubleCollector.toFloatArray());
    java.util.Spliterator.OfDouble spliterator = spliterator();
    int size = intSize(spliterator);
    FloatBuffer buf;
    if (size >= 0) {
        buf = new FloatBuffer(size);
        spliterator.forEachRemaining((DoubleConsumer) buf::addUnsafe);
    } else {
        buf = new FloatBuffer();
        spliterator.forEachRemaining((DoubleConsumer) buf::add);
    }
    return buf.toArray();
}
Also used : FloatBuffer(one.util.streamex.Internals.FloatBuffer) Spliterator(java.util.Spliterator)

Example 38 with Spliterator

use of java.util.Spliterator in project openj9 by eclipse-openj9.

the class StringStreams method testStringCharsSpliteratorSplitting.

@Test
public void testStringCharsSpliteratorSplitting() {
    OfInt s = DIGITS.chars().spliterator();
    ArrayList<OfInt> sArray = new ArrayList<>();
    while (Objects.nonNull(s)) {
        sArray.add(s);
        s = s.trySplit();
    }
    final HashSet<Integer> actualIntegers = new HashSet<>();
    for (OfInt e : sArray) {
        e.forEachRemaining((IntConsumer) i -> actualIntegers.add(Integer.valueOf(i)));
    }
    checkDigits(actualIntegers);
}
Also used : OfInt(java.util.Spliterator.OfInt) HashSet(java.util.HashSet) Objects(java.util.Objects) List(java.util.List) OfInt(java.util.Spliterator.OfInt) IntConsumer(java.util.function.IntConsumer) Assert.assertEquals(org.testng.Assert.assertEquals) Assert.assertTrue(org.testng.Assert.assertTrue) Set(java.util.Set) VersionCheck(org.openj9.test.util.VersionCheck) Test(org.testng.annotations.Test) Spliterator(java.util.Spliterator) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Test(org.testng.annotations.Test)

Example 39 with Spliterator

use of java.util.Spliterator in project openj9 by eclipse-openj9.

the class StringStreams method testStringCharsSpliteratorSingleChar.

@Test
public void testStringCharsSpliteratorSingleChar() {
    Spliterator.OfInt s = SINGLECHAR.chars().spliterator();
    final ArrayList<Integer> actualIntegers = new ArrayList<>();
    s.forEachRemaining((IntConsumer) i -> actualIntegers.add(Integer.valueOf(i)));
    checkDigitsOrdered(SINGLECHAR, actualIntegers);
}
Also used : HashSet(java.util.HashSet) Objects(java.util.Objects) List(java.util.List) OfInt(java.util.Spliterator.OfInt) IntConsumer(java.util.function.IntConsumer) Assert.assertEquals(org.testng.Assert.assertEquals) Assert.assertTrue(org.testng.Assert.assertTrue) Set(java.util.Set) VersionCheck(org.openj9.test.util.VersionCheck) Test(org.testng.annotations.Test) Spliterator(java.util.Spliterator) ArrayList(java.util.ArrayList) OfInt(java.util.Spliterator.OfInt) ArrayList(java.util.ArrayList) Spliterator(java.util.Spliterator) Test(org.testng.annotations.Test)

Example 40 with Spliterator

use of java.util.Spliterator in project streamex by amaembo.

the class IntStreamExTest method testDropWhile.

@Test
public void testDropWhile() {
    assertArrayEquals(new int[] { 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 }, IntStreamEx.range(100).dropWhile(i -> i % 10 < 5).limit(10).toArray());
    assertEquals(100, IntStreamEx.range(100).dropWhile(i -> i % 10 < 0).count());
    assertEquals(0, IntStreamEx.range(100).dropWhile(i -> i % 10 < 10).count());
    assertEquals(OptionalInt.of(0), IntStreamEx.range(100).dropWhile(i -> i % 10 < 0).findFirst());
    assertEquals(OptionalInt.empty(), IntStreamEx.range(100).dropWhile(i -> i % 10 < 10).findFirst());
    java.util.Spliterator.OfInt spltr = IntStreamEx.range(100).dropWhile(i -> i % 10 < 1).spliterator();
    assertTrue(spltr.tryAdvance((int x) -> assertEquals(1, x)));
    Builder builder = IntStream.builder();
    spltr.forEachRemaining(builder);
    assertArrayEquals(IntStreamEx.range(2, 100).toArray(), builder.build().toArray());
}
Also used : IntStream(java.util.stream.IntStream) MethodSorters(org.junit.runners.MethodSorters) Arrays(java.util.Arrays) Builder(java.util.stream.IntStream.Builder) IntUnaryOperator(java.util.function.IntUnaryOperator) OfInt(java.util.PrimitiveIterator.OfInt) Spliterators(java.util.Spliterators) Assert.assertThrows(org.junit.Assert.assertThrows) Scanner(java.util.Scanner) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) IntBinaryOperator(java.util.function.IntBinaryOperator) Random(java.util.Random) TestHelpers.withRandom(one.util.streamex.TestHelpers.withRandom) OptionalInt(java.util.OptionalInt) Function(java.util.function.Function) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Assert.assertSame(org.junit.Assert.assertSame) TestHelpers.intStreamEx(one.util.streamex.TestHelpers.intStreamEx) ByteArrayInputStream(java.io.ByteArrayInputStream) IntBuffer(java.nio.IntBuffer) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) Assert.assertArrayEquals(org.junit.Assert.assertArrayEquals) IntFunction(java.util.function.IntFunction) IntToDoubleFunction(java.util.function.IntToDoubleFunction) TestHelpers.checkSpliterator(one.util.streamex.TestHelpers.checkSpliterator) Assert.assertTrue(org.junit.Assert.assertTrue) IOException(java.io.IOException) Test(org.junit.Test) Collectors(java.util.stream.Collectors) TestHelpers.streamEx(one.util.streamex.TestHelpers.streamEx) UncheckedIOException(java.io.UncheckedIOException) List(java.util.List) IntToLongFunction(java.util.function.IntToLongFunction) StreamEx(one.util.streamex.StreamEx) Assert.assertFalse(org.junit.Assert.assertFalse) IntStreamEx(one.util.streamex.IntStreamEx) BitSet(java.util.BitSet) Comparator(java.util.Comparator) FixMethodOrder(org.junit.FixMethodOrder) Collections(java.util.Collections) Spliterator(java.util.Spliterator) Assert.assertEquals(org.junit.Assert.assertEquals) InputStream(java.io.InputStream) Builder(java.util.stream.IntStream.Builder) TestHelpers.checkSpliterator(one.util.streamex.TestHelpers.checkSpliterator) Spliterator(java.util.Spliterator) Test(org.junit.Test)

Aggregations

Spliterator (java.util.Spliterator)109 List (java.util.List)43 ArrayList (java.util.ArrayList)35 HashSet (java.util.HashSet)31 IntConsumer (java.util.function.IntConsumer)31 Set (java.util.Set)26 Function (java.util.function.Function)24 Objects (java.util.Objects)23 Spliterators (java.util.Spliterators)23 Stream (java.util.stream.Stream)22 Collectors (java.util.stream.Collectors)21 Iterator (java.util.Iterator)20 Map (java.util.Map)20 Consumer (java.util.function.Consumer)20 Comparator (java.util.Comparator)18 LongConsumer (java.util.function.LongConsumer)18 StreamSupport (java.util.stream.StreamSupport)18 Arrays (java.util.Arrays)17 Supplier (java.util.function.Supplier)17 Collections (java.util.Collections)16