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();
}
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();
}
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);
}
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);
}
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());
}
Aggregations