/** * Returns a sequential {@code StreamEx} which elements are elements of * given array in descending order. * * @param <T> the type of stream elements * @param array array to get the elements from * @return the new stream */ public static <T> StreamEx<T> ofReversed(T[] array) { int size = array.length; return IntStreamEx.ofIndices(array).mapToObj(idx -> array[size - idx - 1]); }
/** * Returns a sequential ordered {@code IntStreamEx} whose elements are the * specified values casted to int. * * @param elements the elements of the new stream * @return the new stream * @since 0.2.0 */ public static IntStreamEx of(short... elements) { return of(elements, 0, elements.length); }
@Override public IntStreamEx filter(IntPredicate predicate) { return new IntStreamEx(stream().filter(predicate), context); }
/** * Returns a sequential {@code IntStreamEx} containing an * {@link OptionalInt} value, if present, otherwise returns an empty * {@code IntStreamEx}. * * @param optional the optional to create a stream of * @return a stream with an {@code OptionalInt} value if present, otherwise * an empty stream * @since 0.1.1 */ public static IntStreamEx of(OptionalInt optional) { return optional.isPresent() ? of(optional.getAsInt()) : empty(); }
IntStreamEx callWhile(IntStreamEx stream, IntPredicate predicate, boolean drop) { return stream.delegate(new TakeDrop.TDOfInt(stream.spliterator(), drop, false, predicate)); }
/** * Returns a new stream containing all the elements of the original stream interspersed with * given delimiter. * * <p> * For example, {@code IntStreamEx.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 IntStreamEx intersperse(int delimiter) { return new IntStreamEx(stream().flatMap(s -> IntStreamEx.of(delimiter, s)).skip(1), context); }
Spliterator.OfInt spliterator = (isParallel() ? StreamSupport.intStream(spliterator(), false) : stream()).skip( n).spliterator(); return delegate(spliterator);
@SuppressWarnings("null") private static Message generateMessage() { final String key = RandomStringUtils.randomAlphanumeric(RANDOM.nextInt(6) + 5); final String value = IntStreamEx .of(RANDOM, 100, 0, 256) .mapToObj(Integer::toHexString) .map(Messages::pad) .map(String::toUpperCase) .joining(" "); //$NON-NLS-1$ return new Message(key, value, RANDOM.nextLong(), RANDOM.nextInt(3), System.currentTimeMillis()); }
/** * Returns a {@link BitSet} containing the elements of this stream. * * <p> * This is a terminal operation. * * @return a {@code BitSet} which set bits correspond to the elements of * this stream. * @since 0.2.0 */ public BitSet toBitSet() { return collect(BitSet::new, BitSet::set, BitSet::or); }
private <A> A collectSized(Supplier<A> supplier, ObjIntConsumer<A> accumulator, BiConsumer<A, A> combiner, IntFunction<A> sizedSupplier, ObjIntConsumer<A> sizedAccumulator) { if (isParallel()) return collect(supplier, accumulator, combiner); java.util.Spliterator.OfInt spliterator = spliterator(); long size = spliterator.getExactSizeIfKnown(); A intermediate; if (size >= 0 && size <= Integer.MAX_VALUE) { intermediate = sizedSupplier.apply((int) size); spliterator.forEachRemaining((IntConsumer) i -> sizedAccumulator.accept(intermediate, i)); } else { intermediate = supplier.get(); spliterator.forEachRemaining((IntConsumer) i -> accumulator.accept(intermediate, i)); } return intermediate; }
Spliterator.OfInt spliterator = spliterator(); long size = spliterator.getExactSizeIfKnown(); IntBuffer buf = new IntBuffer(size >= 0 && size <= Integer.MAX_VALUE ? (int) size : INITIAL_SIZE); delegate(spliterator).forEachOrdered(i -> buf.add(buf.size == 0 ? i : accumulator.applyAsInt(buf.data[buf.size - 1], i))); return buf.toArray();
/** * Returns a sequential {@link IntStreamEx} created from given * {@link java.util.Spliterator.OfInt}. * * @param spliterator a spliterator to create the stream from. * @return the new stream * @since 0.3.4 */ public static IntStreamEx of(Spliterator.OfInt spliterator) { return new IntStreamEx(spliterator, StreamContext.SEQUENTIAL); }
/** * Returns an object-valued {@link StreamEx} consisting of the elements of * given array corresponding to the indices which appear in this stream. * * <p> * This is an intermediate operation. * * @param <U> the element type of the new stream * @param array the array to take the elements from * @return the new stream * @since 0.1.2 */ public <U> StreamEx<U> elements(U[] array) { return mapToObj(idx -> array[idx]); }
/** * Returns a sequential ordered {@code IntStreamEx} 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 IntStreamEx} for the range of {@code int} * elements * @throws IllegalArgumentException if step is zero * @see IntStreamEx#range(int, int) * @since 0.4.0 */ public static IntStreamEx range(int startInclusive, int endExclusive, int step) { int endInclusive = endExclusive - Integer.signum(step); if (endInclusive > endExclusive && step > 0 || endInclusive < endExclusive && step < 0) return empty(); return rangeClosed(startInclusive, endInclusive, step); }
throw new IllegalArgumentException("step = 0"); if (step == 1) return seq(IntStream.rangeClosed(startInclusive, endInclusive)); if (step == -1) { return seq(IntStream.rangeClosed(endInclusive, startInclusive).map(x -> sum - x)); return empty(); int limit = (endInclusive - startInclusive) * Integer.signum(step); limit = Integer.divideUnsigned(limit, Math.abs(step)); return seq(IntStream.rangeClosed(0, limit).map(x -> x * step + startInclusive));
/** * Returns a {@code char[]} array containing the elements of this stream * which are converted to chars using {@code (char)} cast operation. * * <p> * This is a terminal operation. * * @return an array containing the elements of this stream * @since 0.3.0 */ public char[] toCharArray() { return collectSized(CharBuffer::new, CharBuffer::add, CharBuffer::addAll, CharBuffer::new, CharBuffer::addUnsafe).toArray(); }
/** * 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 IntStream#anyMatch(IntPredicate) */ public boolean has(int value) { return anyMatch(x -> x == value); }
/** * 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(IntPredicate) * @since 0.4.0 */ public OptionalLong indexOf(int value) { return boxed().indexOf(i -> i == value); }