@Override public LongStreamEx skip(long n) { return new LongStreamEx(stream().skip(n), context); }
/** * Returns true if this stream contains the specified value * * <p> * This is a short-circuiting terminal operation. * * @param value the value too look for in the stream * @return true if this stream contains the specified value * @see LongStream#anyMatch(LongPredicate) */ public boolean has(long value) { return anyMatch(x -> x == value); }
@Override public LongSummaryStatistics summaryStatistics() { return collect(LongSummaryStatistics::new, LongSummaryStatistics::accept, LongSummaryStatistics::combine); }
/** * Returns an {@link OptionalLong} describing the first element of this * stream, which matches given predicate, or an empty {@code OptionalLong} * if there's no matching element. * * <p> * This is a short-circuiting terminal operation. * * @param predicate a * <a href="package-summary.html#NonInterference">non-interfering * </a>, <a href="package-summary.html#Statelessness">stateless</a> * predicate which returned value should match * @return an {@code OptionalLong} describing the first matching element of * this stream, or an empty {@code OptionalLong} if there's no * matching element * @see #findFirst() */ public OptionalLong findFirst(LongPredicate predicate) { return filter(predicate).findFirst(); }
/** * Returns an {@link OptionalLong} describing some element of the stream, * which matches given predicate, or an empty {@code OptionalLong} if * there's no matching element. * * <p> * This is a short-circuiting terminal operation. * * <p> * The behavior of this operation is explicitly nondeterministic; it is free * to select any element in the stream. This is to allow for maximal * performance in parallel operations; the cost is that multiple invocations * on the same source may not return the same result. (If a stable result is * desired, use {@link #findFirst(LongPredicate)} instead.) * * @param predicate a * <a href="package-summary.html#NonInterference">non-interfering * </a>, <a href="package-summary.html#Statelessness">stateless</a> * predicate which returned value should match * @return an {@code OptionalLong} describing some matching element of this * stream, or an empty {@code OptionalLong} if there's no matching * element * @see #findAny() * @see #findFirst(LongPredicate) */ public OptionalLong findAny(LongPredicate predicate) { return filter(predicate).findAny(); }
LongStreamEx callWhile(LongStreamEx stream, LongPredicate predicate, boolean drop) { return stream.delegate(new TakeDrop.TDOfLong(stream.spliterator(), drop, false, predicate)); }
Spliterator.OfLong spliterator = (isParallel() ? StreamSupport.longStream(spliterator(), false) : stream()) .skip(n).spliterator(); return delegate(spliterator);
/** * Returns a new stream containing all the elements of the original stream interspersed with * given delimiter. * * <p> * For example, {@code LongStreamEx.of(1, 2, 3).intersperse(4)} will yield a stream containing * five elements: 1, 4, 2, 4, 3. * * <p> * This is an <a href="package-summary.html#StreamOps">intermediate operation</a>. * * @param delimiter a delimiter to be inserted between each pair of elements * @return the new stream * @since 0.6.6 */ public LongStreamEx intersperse(int delimiter) { return new LongStreamEx(stream().flatMap(s -> LongStreamEx.of(delimiter, s)).skip(1), context); }
Spliterator.OfLong spliterator = spliterator(); long size = spliterator.getExactSizeIfKnown(); LongBuffer buf = new LongBuffer(size >= 0 && size <= Integer.MAX_VALUE ? (int) size : INITIAL_SIZE); delegate(spliterator).forEachOrdered(i -> buf.add(buf.size == 0 ? i : accumulator.applyAsLong(buf.data[buf.size - 1], i))); return buf.toArray();
/** * Returns a sequential {@link LongStreamEx} created from given * {@link java.util.Spliterator.OfLong}. * * @param spliterator a spliterator to create the stream from. * @return the new stream * @since 0.3.4 */ public static LongStreamEx of(Spliterator.OfLong spliterator) { return new LongStreamEx(spliterator, StreamContext.SEQUENTIAL); }
/** * Returns a sequential ordered {@code LongStreamEx} from * {@code startInclusive} (inclusive) to {@code endExclusive} (exclusive) by * the specified incremental step. The negative step values are also * supported. In this case the {@code startInclusive} should be greater than * {@code endExclusive}. * * @param startInclusive the (inclusive) initial value * @param endExclusive the exclusive upper bound * @param step the non-zero value which designates the difference between * the consecutive values of the resulting stream. * @return a sequential {@code LongStreamEx} for the range of {@code long} * elements * @throws IllegalArgumentException if step is zero * @see LongStreamEx#range(long, long) * @since 0.4.0 */ public static LongStreamEx range(long startInclusive, long endExclusive, long step) { long endInclusive = endExclusive - Long.signum(step); if (endInclusive > endExclusive && step > 0 || endInclusive < endExclusive && step < 0) return empty(); return rangeClosed(startInclusive, endInclusive, step); }
/** * Returns a sequential {@code LongStreamEx} containing an * {@link OptionalLong} value, if present, otherwise returns an empty * {@code LongStreamEx}. * * @param optional the optional to create a stream of * @return a stream with an {@code OptionalLong} value if present, otherwise * an empty stream * @since 0.1.1 */ public static LongStreamEx of(OptionalLong optional) { return optional.isPresent() ? of(optional.getAsLong()) : empty(); }
/** * Returns a stream consisting of the elements of this stream that strictly * less than the specified value. * * <p> * This is an intermediate operation. * * @param value a value to compare to * @return the new stream * @since 0.2.3 */ public LongStreamEx less(long value) { return filter(val -> val < value); }
throw new IllegalArgumentException("step = 0"); if (step == 1) return seq(LongStream.rangeClosed(startInclusive, endInclusive)); if (step == -1) { return seq(LongStream.rangeClosed(endInclusive, startInclusive).map(x -> sum - x)); return empty(); long limit = (endInclusive - startInclusive) * Long.signum(step); limit = Long.divideUnsigned(limit, Math.abs(step)); return seq(LongStream.rangeClosed(0, limit).map(x -> x * step + startInclusive));
/** * Returns an {@link OptionalLong} describing the zero-based index of the * first element of this stream, which equals to the given value, or an * empty {@code OptionalLong} if there's no matching element. * * <p> * This is a short-circuiting terminal operation. * * @param value a value to look for * @return an {@code OptionalLong} describing the index of the first * matching element of this stream, or an empty {@code OptionalLong} * if there's no matching element. * @see #indexOf(LongPredicate) * @since 0.4.0 */ public OptionalLong indexOf(long value) { return boxed().indexOf(i -> i == value); }
forEachOrdered(t -> box[0] = accumulator.applyAsLong(box[0], t)); return box[0];
/** * Returns a stream where the first element is the replaced with the result * of applying the given function while the other elements are left intact. * * <p> * This is a <a href="package-summary.html#StreamOps">quasi-intermediate * operation</a>. * * @param mapper a * <a href="package-summary.html#NonInterference">non-interfering * </a>, <a href="package-summary.html#Statelessness">stateless</a> * function to apply to the first element * @return the new stream * @since 0.4.1 */ public LongStreamEx mapFirst(LongUnaryOperator mapper) { return delegate(new PairSpliterator.PSOfLong((a, b) -> b, mapper, spliterator(), PairSpliterator.MODE_MAP_FIRST)); }
private static LongStreamEx seq(LongStream stream) { return new LongStreamEx(stream, StreamContext.SEQUENTIAL); }
/** * Returns a stream consisting of the elements of this stream that less than * or equal to the specified value. * * <p> * This is an intermediate operation. * * @param value a value to compare to * @return the new stream * @since 0.2.3 */ public LongStreamEx atMost(long value) { return filter(val -> val <= value); }