Java : IntStream with Examples

IntStream (Java SE 21 & JDK 21) with Examples.
You will find code examples on most IntStream methods.


Summary

A sequence of primitive int-valued elements supporting sequential and parallel aggregate operations. This is the int primitive specialization of Stream.

Class diagram

final var stream = IntStream.range(0, 5);

System.out.println("-- forEach --");
stream.forEach(v -> {
    System.out.println(v);
});

// Result
// ↓
//-- forEach --
//0
//1
//2
//3
//4
final var stream = IntStream.of(10, 20, 30);

final var ret = stream.toArray();
System.out.println(Arrays.toString(ret)); // [10, 20, 30]

Methods

boolean allMatch (IntPredicate predicate)

Returns whether all elements of this stream match the provided predicate.

final var predicate = new IntPredicate() {
    @Override
    public boolean test(int value) {
        // Check if the number is even.
        return value % 2 == 0;
    }
};

{
    final var stream = IntStream.of(0, 2, 4);

    final var ret = stream.allMatch(predicate);
    System.out.println(ret); // true
}
{
    final var stream = IntStream.of(0, 1, 2, 3, 4);

    final var ret = stream.allMatch(predicate);
    System.out.println(ret); // false
}

boolean anyMatch (IntPredicate predicate)

Returns whether any elements of this stream match the provided predicate.

final var predicate = new IntPredicate() {
    @Override
    public boolean test(int value) {
        // Check if the number is even.
        return value % 2 == 0;
    }
};

{
    final var stream = IntStream.of(0, 2, 4);

    final var ret = stream.anyMatch(predicate);
    System.out.println(ret); // true
}
{
    final var stream = IntStream.of(1, 2, 3);

    final var ret = stream.anyMatch(predicate);
    System.out.println(ret); // true
}
{
    final var stream = IntStream.of(1, 3, 5);

    final var ret = stream.anyMatch(predicate);
    System.out.println(ret); // false
}

DoubleStream asDoubleStream ()

Returns a DoubleStream consisting of the elements of this stream, converted to double.

final var stream = IntStream.of(1, 2, 3);
final var doubleStream = stream.asDoubleStream().map(v -> v / 2.0);

final var ret = doubleStream.toArray();
System.out.println(Arrays.toString(ret)); // [0.5, 1.0, 1.5]

LongStream asLongStream ()

Returns a LongStream consisting of the elements of this stream, converted to long.

final var stream = IntStream.of(1, 2, 3);
final var longStream = stream.asLongStream().map(v -> v * 10000000000L);

final var ret = longStream.toArray();

// [10000000000, 20000000000, 30000000000]
System.out.println(Arrays.toString(ret));

OptionalDouble average ()

Returns an OptionalDouble describing the arithmetic mean of elements of this stream, or an empty optional if this stream is empty.

final var stream = IntStream.of(1, 2, 3, 4, 5, 6);

final var opt = stream.average();
opt.ifPresent(value -> {
    System.out.println(value); // 3.5
});
final var opt = IntStream.empty().average();
System.out.println(opt.isEmpty()); // true

Stream<Integer> boxed ()

Returns a Stream consisting of the elements of this stream, each boxed to an Integer.

final Stream<Integer> stream = IntStream.range(0, 5).boxed();

final var ret = stream.toList();
System.out.println(ret); // [0, 1, 2, 3, 4]

static IntStream.Builder builder ()

Returns a builder for an IntStream.

final var builder = IntStream.builder();
builder.add(0).add(2).add(4);

final var stream = builder.build();

final var ret = stream.toArray();
System.out.println(Arrays.toString(ret)); // [0, 2, 4]

<R> R collect (Supplier<R> supplier, ObjIntConsumer<R> accumulator, BiConsumer<R,R> combiner)

Performs a mutable reduction operation on the elements of this stream.

final var supplier = new Supplier<List<String>>() {
    @Override
    public List<String> get() {
        System.out.println("-- supplier --");
        return new ArrayList<>();
    }
};

final var accumulator = new ObjIntConsumer<List<String>>() {
    @Override
    public void accept(List<String> list, int value) {
        System.out.println("-- accumulator --");
        System.out.println("  list = " + list);
        System.out.println("  value = " + value);

        list.add("%#x".formatted(value));
    }
};

final var combiner = new BiConsumer<List<String>, List<String>>() {
    @Override
    public void accept(List<String> list1, List<String> list2) {
        System.out.println("-- combiner --");
        System.out.println("  list1 = " + list1);
        System.out.println("  list2 = " + list2);

        list1.addAll(list2);
    }
};

{
    final var stream = IntStream.of(1, 100, 255);

    final var ret = stream.collect(supplier, accumulator, combiner);
    System.out.println("collect = " + ret);

    // Result
    // ↓
    //-- supplier --
    //-- accumulator --
    //  list = []
    //  value = 1
    //-- accumulator --
    //  list = [0x1]
    //  value = 100
    //-- accumulator --
    //  list = [0x1, 0x64]
    //  value = 255
    //collect = [0x1, 0x64, 0xff]
}
{
    final var parallelStream = IntStream.of(1, 100, 255).parallel();
    System.out.println(parallelStream.isParallel()); // true

    final var ret = parallelStream.collect(supplier, accumulator, combiner);
    System.out.println("collect = " + ret);

    // Result
    // ↓
    //-- supplier --
    //-- accumulator --
    //  list = []
    //  value = 100
    //-- supplier --
    //-- accumulator --
    //  list = []
    //  value = 255
    //-- supplier --
    //-- accumulator --
    //  list = []
    //  value = 1
    //-- combiner --
    //  list1 = [0x64]
    //  list2 = [0xff]
    //-- combiner --
    //  list1 = [0x1]
    //  list2 = [0x64, 0xff]
    //collect = [0x1, 0x64, 0xff]
}

static IntStream concat (IntStream a, IntStream b)

Creates a lazily concatenated stream whose elements are all the elements of the first stream followed by all the elements of the second stream.

final var stream1 = IntStream.of(1, 2, 3);
final var stream2 = IntStream.of(100, 200, 300);

System.out.println(stream1.isParallel()); // false
System.out.println(stream2.isParallel()); // false

final var concatStream = IntStream.concat(stream1, stream2);

System.out.println(concatStream.isParallel()); // false

final var ret = concatStream.toArray();
System.out.println(Arrays.toString(ret)); // [1, 2, 3, 100, 200, 300]
final var stream1 = IntStream.of(1, 2, 3).parallel();
final var stream2 = IntStream.of(100, 200, 300).parallel();

System.out.println(stream1.isParallel()); // true
System.out.println(stream2.isParallel()); // true

final var concatStream = IntStream.concat(stream1, stream2);

System.out.println(concatStream.isParallel()); // true

final var ret = concatStream.toArray();
System.out.println(Arrays.toString(ret)); // [1, 2, 3, 100, 200, 300]

long count ()

Returns the count of elements in this stream.

final var stream = IntStream.of(0, 2, 4, 6);
System.out.println(stream.count()); // 4
final var stream = IntStream.empty();
System.out.println(stream.count()); // 0

IntStream distinct ()

Returns a stream consisting of the distinct elements of this stream.

final var stream = IntStream.of(1, 2, 2, 3, 3, 3);

final var ret = stream.distinct().toArray();
System.out.println(Arrays.toString(ret)); // [1, 2, 3]

default IntStream dropWhile (IntPredicate predicate)

Returns, if this stream is ordered, a stream consisting of the remaining elements of this stream after dropping the longest prefix of elements that match the given predicate.

final var stream = IntStream.of(1, 2, 3, 4, 5);

final var predicate = new IntPredicate() {
    @Override
    public boolean test(int value) {
        return value != 3;
    }
};

final var ret = stream.dropWhile(predicate).toArray();
System.out.println(Arrays.toString(ret)); // [3, 4, 5]
final var stream = IntStream.of(-1, -2, -3, -4, -5);

final var ret = stream.dropWhile(v -> v != -4).toArray();
System.out.println(Arrays.toString(ret)); // [-4, -5]

static IntStream empty ()

Returns an empty sequential IntStream.

final var stream = IntStream.empty();
System.out.println(stream.count()); // 0
final var stream = IntStream.empty();
final var array = stream.toArray();
System.out.println(Arrays.toString(array)); // []

IntStream filter (IntPredicate predicate)

Returns a stream consisting of the elements of this stream that match the given predicate.

final var stream = IntStream.of(0, 1, 2, 3, 4, 5);

final var predicate = new IntPredicate() {
    @Override
    public boolean test(int value) {
        // Check if the number is even.
        return value % 2 == 0;
    }
};

final var ret = stream.filter(predicate).toArray();
System.out.println(Arrays.toString(ret)); // [0, 2, 4]
final var stream = IntStream.of(0, 1, 2, 3, 4, 5);

final var ret = stream.filter(v -> v % 2 == 1).toArray();
System.out.println(Arrays.toString(ret)); // [1, 3, 5]

OptionalInt findAny ()

Returns an OptionalInt describing some element of the stream, or an empty OptionalInt if the stream is empty.

int count1 = 0;
int count2 = 0;
int count3 = 0;

for (int i = 0; i < 1000; i++) {

    final var stream = IntStream.of(1, 2, 3).parallel();
    System.out.println(stream.isParallel()); // true

    // 1 or 2 or 3
    final var opt = stream.findAny();
    if (opt.isEmpty()) {
        throw new IllegalStateException();
    }

    switch (opt.orElseThrow()) {
        case 1 -> count1++;
        case 2 -> count2++;
        case 3 -> count3++;
    }
}

System.out.println("count1 : " + count1);
System.out.println("count2 : " + count2);
System.out.println("count3 : " + count3);

// Result
// ↓
//count1 : 183
//count2 : 814
//count3 : 3
final var stream = IntStream.empty();

final var opt = stream.findAny();
System.out.println(opt.isEmpty()); // true

OptionalInt findFirst ()

Returns an OptionalInt describing the first element of this stream, or an empty OptionalInt if the stream is empty.

final var stream = IntStream.of(1, 2, 3);

final var opt = stream.findFirst();
opt.ifPresent(value -> {
    System.out.println(value); // 1
});
// parallel
final var stream = IntStream.of(1, 2, 3).parallel();
System.out.println(stream.isParallel()); // true

final var opt = stream.findFirst();
opt.ifPresent(value -> {
    System.out.println(value); // 1
});
int count1 = 0;
int count2 = 0;
int count3 = 0;

for (int i = 0; i < 1000; i++) {

    // unordered and parallel
    final var stream = IntStream.of(1, 2, 3).unordered().parallel();

    // 1 or 2 or 3
    final var opt = stream.findFirst();
    if (opt.isEmpty()) {
        throw new IllegalStateException();
    }

    switch (opt.orElseThrow()) {
        case 1 -> count1++;
        case 2 -> count2++;
        case 3 -> count3++;
    }
}

System.out.println("count1 : " + count1);
System.out.println("count2 : " + count2);
System.out.println("count3 : " + count3);

// Result
// ↓
//count1 : 137
//count2 : 856
//count3 : 7
final var stream = IntStream.empty();

final var opt = stream.findFirst();
System.out.println(opt.isEmpty()); // true

IntStream flatMap (IntFunction<? extends IntStream> mapper)

Returns a stream consisting of the results of replacing each element of this stream with the contents of a mapped stream produced by applying the provided mapping function to each element.

final var stream = IntStream.of(100, 200, 300);

final var mapper = new IntFunction<IntStream>() {
    @Override
    public IntStream apply(int value) {
        return IntStream.of(value, value + 1, value + 2);
    }
};

final var ret = stream.flatMap(mapper).toArray();

// [100, 101, 102, 200, 201, 202, 300, 301, 302]
System.out.println(Arrays.toString(ret));

void forEach (IntConsumer action)

Performs an action for each element of this stream.

Please see also : forEachOrdered(IntConsumer action)

final var stream = IntStream.of(10, 20, 30);

final var action = new IntConsumer() {
    @Override
    public void accept(int value) {
        System.out.println(value);
    }
};

System.out.println("-- forEach --");
stream.forEach(action);

// Result
// ↓
//-- forEach --
//10
//20
//30
// An example with a lambda expression.
final var stream = IntStream.of(-10, -20, -30);

System.out.println("-- forEach --");
stream.forEach(v -> {
    System.out.println(v);
});

// Result
// ↓
//-- forEach --
//-10
//-20
//-30
final var stream = IntStream.of(10, 20, 30, 40, 50).parallel();
System.out.println(stream.isParallel()); // true

System.out.println("-- forEach --");
stream.forEach(v -> {
    System.out.println(v);
});

// Result
// ↓
//-- forEach --
//30
//50
//40
//10
//20

void forEachOrdered (IntConsumer action)

Performs an action for each element of this stream, guaranteeing that each element is processed in encounter order for streams that have a defined encounter order.

final var stream = IntStream.of(10, 20, 30);

final var action = new IntConsumer() {
    @Override
    public void accept(int value) {
        System.out.println(value);
    }
};

System.out.println("-- forEach --");
stream.forEachOrdered(action);

// Result
// ↓
//-- forEach --
//10
//20
//30
final var stream = IntStream.of(10, 20, 30, 40, 50).parallel();
System.out.println(stream.isParallel()); // true

System.out.println("-- forEach --");
stream.forEachOrdered(v -> {
    System.out.println(v);
});

// Result
// ↓
//-- forEach --
//10
//20
//30
//40
//50

static IntStream generate (IntSupplier s)

Returns an infinite sequential unordered stream where each element is generated by the provided IntSupplier.

final var supplier = new IntSupplier() {
    int count = 0;

    @Override
    public int getAsInt() {
        count += 10;
        return count;
    }
};

final var stream = IntStream.generate(supplier);

final var ret = stream.limit(5).toArray();
System.out.println(Arrays.toString(ret)); // [10, 20, 30, 40, 50]

static IntStream iterate (int seed, IntPredicate hasNext, IntUnaryOperator next)

Returns a sequential ordered IntStream produced by iterative application of the given next function to an initial element, conditioned on satisfying the given hasNext predicate.

final var hasNext = new IntPredicate() {
    @Override
    public boolean test(int value) {
        return value <= 100;
    }
};

final var next = new IntUnaryOperator() {
    @Override
    public int applyAsInt(int operand) {
        return operand * 2;
    }
};

final var stream = IntStream.iterate(1, hasNext, next);

final var ret = stream.toArray();
System.out.println(Arrays.toString(ret)); // [1, 2, 4, 8, 16, 32, 64]

static IntStream iterate (int seed, IntUnaryOperator f)

Returns an infinite sequential ordered IntStream produced by iterative application of a function f to an initial element seed, producing a Stream consisting of seed, f(seed), f(f(seed)), etc.

final var next = new IntUnaryOperator() {
    @Override
    public int applyAsInt(int operand) {
        return operand * 2;
    }
};

final var stream = IntStream.iterate(1, next);
final var ret = stream.limit(10).toArray();

// [1, 2, 4, 8, 16, 32, 64, 128, 256, 512]
System.out.println(Arrays.toString(ret));

IntStream limit (long maxSize)

Returns a stream consisting of the elements of this stream, truncated to be no longer than maxSize in length.

final var stream = IntStream.of(10, 20, 30, 40, 50, 60);

final var ret = stream.limit(3).toArray();
System.out.println(Arrays.toString(ret)); // [10, 20, 30]

IntStream map (IntUnaryOperator mapper)

Returns a stream consisting of the results of applying the given function to the elements of this stream.

final var stream = IntStream.of(1, 2, 3);

final var mapper = new IntUnaryOperator() {
    @Override
    public int applyAsInt(int operand) {
        return operand * 2;
    }
};

final var ret = stream.map(mapper).toArray();
System.out.println(Arrays.toString(ret)); // [2, 4, 6]
final var stream = IntStream.range(0, 4);

final var ret = stream.map(v -> v * 100).toArray();
System.out.println(Arrays.toString(ret)); // [0, 100, 200, 300]

default IntStream mapMulti (IntStream.IntMapMultiConsumer mapper)

Returns a stream consisting of the results of replacing each element of this stream with multiple elements, specifically zero or more elements.

final var stream = IntStream.of(100, 200, 300);

final var mapper = new IntStream.IntMapMultiConsumer() {
    @Override
    public void accept(int value, IntConsumer ic) {
        ic.accept(value);
        ic.accept(value + 1);
        ic.accept(value + 2);
    }
};

final var ret = stream.mapMulti(mapper).toArray();

// [100, 101, 102, 200, 201, 202, 300, 301, 302]
System.out.println(Arrays.toString(ret));

DoubleStream mapToDouble (IntToDoubleFunction mapper)

Returns a DoubleStream consisting of the results of applying the given function to the elements of this stream.

final var stream = IntStream.of(100, 125, 200);

final var mapper = new IntToDoubleFunction() {
    @Override
    public double applyAsDouble(int value) {
        return value / 2.0;
    }
};

final var ret = stream.mapToDouble(mapper).toArray();
System.out.println(Arrays.toString(ret)); // [50.0, 62.5, 100.0]

LongStream mapToLong (IntToLongFunction mapper)

Returns a LongStream consisting of the results of applying the given function to the elements of this stream.

final var stream = IntStream.of(1, 2, 3);

final var mapper = new IntToLongFunction() {
    @Override
    public long applyAsLong(int value) {
        return value * 10000000000L;
    }
};

final var ret = stream.mapToLong(mapper).toArray();

// [10000000000, 20000000000, 30000000000]
System.out.println(Arrays.toString(ret));

<U> Stream<U> mapToObj (IntFunction<? extends U> mapper)

Returns an object-valued Stream consisting of the results of applying the given function to the elements of this stream.

final var stream = IntStream.of(1, 2, 3);

final var mapper = new IntFunction<String>() {
    @Override
    public String apply(int value) {
        return "num=" + value;
    }
};

final var ret = stream.mapToObj(mapper).toList();
System.out.println(ret); // [num=1, num=2, num=3]

OptionalInt max ()

Returns an OptionalInt describing the maximum element of this stream, or an empty optional if this stream is empty.

final var stream = IntStream.of(10, 5, 100, 30);

final var opt = stream.max();
opt.ifPresent(value -> {
    System.out.println(value); // 100
});
final var stream = IntStream.empty();

final var opt = stream.max();
System.out.println(opt.isEmpty()); // true

OptionalInt min ()

Returns an OptionalInt describing the minimum element of this stream, or an empty optional if this stream is empty.

final var stream = IntStream.of(10, 5, 100, 30);

final var opt = stream.min();
opt.ifPresent(value -> {
    System.out.println(value); // 5
});
final var stream = IntStream.empty();

final var opt = stream.min();
System.out.println(opt.isEmpty()); // true

boolean noneMatch (IntPredicate predicate)

Returns whether no elements of this stream match the provided predicate.

final var predicate = new IntPredicate() {
    @Override
    public boolean test(int value) {
        // Check if the number is even.
        return value % 2 == 0;
    }
};

{
    final var stream = IntStream.of(0, 2, 4);

    final var ret = stream.noneMatch(predicate);
    System.out.println(ret); // false
}
{
    final var stream = IntStream.of(1, 2, 3);

    final var ret = stream.noneMatch(predicate);
    System.out.println(ret); // false
}
{
    final var stream = IntStream.of(1, 3, 5);

    final var ret = stream.noneMatch(predicate);
    System.out.println(ret); // true
}

static IntStream of (int t)

Returns a sequential IntStream containing a single element.

final var stream = IntStream.of(0);

final var ret = stream.toArray();
System.out.println(Arrays.toString(ret)); // [0]
final var stream = IntStream.of(123);

final var ret = stream.toArray();
System.out.println(Arrays.toString(ret)); // [123]

static IntStream of (int... values)

Returns a sequential ordered stream whose elements are the specified values.

final var stream = IntStream.of(0, 2, 4, 6, 8, 10);

final var ret = stream.toArray();
System.out.println(Arrays.toString(ret)); // [0, 2, 4, 6, 8, 10]

IntStream peek (IntConsumer action)

Returns a stream consisting of the elements of this stream, additionally performing the provided action on each element as elements are consumed from the resulting stream.

final var stream = IntStream.of(10, 20, 30);

final var action = new IntConsumer() {
    @Override
    public void accept(int value) {
        System.out.println("peek : value = " + value);
    }
};

final var array = stream.peek(action).toArray();
System.out.println("array = " + Arrays.toString(array));

// Result
// ↓
//peek : value = 10
//peek : value = 20
//peek : value = 30
//array = [10, 20, 30]

static IntStream range (int startInclusive, int endExclusive)

Returns a sequential ordered IntStream from startInclusive (inclusive) to endExclusive (exclusive) by an incremental step of 1.

final var stream = IntStream.range(0, 10);
final var ret = stream.toArray();

// [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
System.out.println(Arrays.toString(ret));
final var stream = IntStream.range(-5, 5);
final var ret = stream.toArray();

// [-5, -4, -3, -2, -1, 0, 1, 2, 3, 4]
System.out.println(Arrays.toString(ret));

static IntStream rangeClosed (int startInclusive, int endInclusive)

Returns a sequential ordered IntStream from startInclusive (inclusive) to endInclusive (inclusive) by an incremental step of 1.

final var stream = IntStream.rangeClosed(0, 10);
final var ret = stream.toArray();

// [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
System.out.println(Arrays.toString(ret));
final var stream = IntStream.rangeClosed(-5, 5);
final var ret = stream.toArray();

// [-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5]
System.out.println(Arrays.toString(ret));

int reduce (int identity, IntBinaryOperator op)

Performs a reduction on the elements of this stream, using the provided identity value and an associative accumulation function, and returns the reduced value.

final var stream = IntStream.of(1, 2, 3, 4);
final var identity = 0;

final var op = new IntBinaryOperator() {
    @Override
    public int applyAsInt(int left, int right) {
        if (identity == left) {
            return right;
        }

        return left + right;
    }
};

// 1 + 2 + 3 + 4 = 10
final var ret = stream.reduce(identity, op);
System.out.println(ret); // 10
final var stream = IntStream.empty();
final var identity = 0;

final var ret = stream.reduce(identity, (left, right) -> {
    if (identity == left) {
        return right;
    }

    return left + right;
});

System.out.println(ret); // 0

OptionalInt reduce (IntBinaryOperator op)

Performs a reduction on the elements of this stream, using an associative accumulation function, and returns an OptionalInt describing the reduced value, if any.

final var stream = IntStream.of(1, 2, 3, 4);

final var op = new IntBinaryOperator() {
    @Override
    public int applyAsInt(int left, int right) {
        return left + right;
    }
};

// 1 + 2 + 3 + 4 = 10
final var opt = stream.reduce(op);
opt.ifPresent(value -> {
    System.out.println(value); // 10
});
final var stream = IntStream.empty();

final var opt = stream.reduce((left, right) -> left + right);
System.out.println(opt.isEmpty()); // true

IntStream skip (long n)

Returns a stream consisting of the remaining elements of this stream after discarding the first n elements of the stream.

final var stream = IntStream.of(10, 20, 30, 40, 50, 60, 70);

final var ret = stream.skip(4).toArray();
System.out.println(Arrays.toString(ret)); // [50, 60, 70]

IntStream sorted ()

Returns a stream consisting of the elements of this stream in sorted order.

final var stream = IntStream.of(5, 3, 2, 4, 1);

final var ret = stream.sorted().toArray();
System.out.println(Arrays.toString(ret)); // [1, 2, 3, 4, 5]
final var stream = IntStream.of(5, 3, 2, 4, 1).parallel();
System.out.println(stream.isParallel()); // true

final var ret = stream.sorted().toArray();
System.out.println(Arrays.toString(ret)); // [1, 2, 3, 4, 5]

int sum ()

Returns the sum of elements in this stream.

final var stream = IntStream.of(1, 2, 3, 4, 5);
System.out.println(stream.sum()); // 15
final var stream = IntStream.of(-10, -20, -30);
System.out.println(stream.sum()); // -60

IntSummaryStatistics summaryStatistics ()

Returns an IntSummaryStatistics describing various summary data about the elements of this stream.

final var stream = IntStream.of(1, 2, 3, 4, 5);
final var ret = stream.summaryStatistics();

// IntSummaryStatistics{count=5, sum=15, min=1, average=3.000000, max=5}
System.out.println(ret);

default IntStream takeWhile (IntPredicate predicate)

Returns, if this stream is ordered, a stream consisting of the longest prefix of elements taken from this stream that match the given predicate.

final var stream = IntStream.of(1, 2, 3, 4, 5);

final var predicate = new IntPredicate() {
    @Override
    public boolean test(int value) {
        return value != 4;
    }
};

final var ret = stream.takeWhile(predicate).toArray();
System.out.println(Arrays.toString(ret)); // [1, 2, 3]
final var stream = IntStream.of(-1, -2, -3, -4, -5);

final var ret = stream.takeWhile(v -> v != -3).toArray();
System.out.println(Arrays.toString(ret)); // [-1, -2]

int[] toArray ()

Returns an array containing the elements of this stream.

final var stream = IntStream.of(1, 2, 3, 4, 5);
final int[] ret = stream.toArray();

System.out.println(Arrays.toString(ret)); // [1, 2, 3, 4, 5]

Methods declared in BaseStream

close, isParallel, iterator, onClose, parallel, sequential, spliterator, unordered

Please see the link below.


Related posts

To top of page