Java : LongStream with Examples

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


Summary

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

Class diagram

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

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

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

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

Methods

boolean allMatch (LongPredicate predicate)

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

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

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

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

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

boolean anyMatch (LongPredicate predicate)

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

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

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

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

    final var ret = stream.anyMatch(predicate);
    System.out.println(ret); // true
}
{
    final var stream = LongStream.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 = LongStream.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]

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 = LongStream.of(1, 2, 3, 4, 5, 6);

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

Stream<Long> boxed ()

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

final Stream<Long> stream = LongStream.range(0, 5).boxed();

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

static LongStream.Builder builder ()

Returns a builder for a LongStream.

final var builder = LongStream.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, ObjLongConsumer<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 ObjLongConsumer<List<String>>() {
    @Override
    public void accept(List<String> list, long 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 = LongStream.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 = LongStream.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 LongStream concat (LongStream a, LongStream 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 = LongStream.of(1, 2, 3);
final var stream2 = LongStream.of(100, 200, 300);

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

final var concatStream = LongStream.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 = LongStream.of(1, 2, 3).parallel();
final var stream2 = LongStream.of(100, 200, 300).parallel();

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

final var concatStream = LongStream.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 = LongStream.of(0, 2, 4, 6);
System.out.println(stream.count()); // 4
final var stream = LongStream.empty();
System.out.println(stream.count()); // 0

LongStream distinct ()

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

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

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

default LongStream dropWhile (LongPredicate 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 = LongStream.of(1, 2, 3, 4, 5);

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

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

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

static LongStream empty ()

Returns an empty sequential LongStream.

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

LongStream filter (LongPredicate predicate)

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

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

final var predicate = new LongPredicate() {
    @Override
    public boolean test(long 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 = LongStream.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]

OptionalLong findAny ()

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

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

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

    final var stream = LongStream.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();
    }

    final var value = opt.orElseThrow();
    if (value == 1) {
        count1++;
    } else if (value == 2) {
        count2++;
    } else if (value == 3) {
        count3++;
    }
}

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

// Result
// ↓
//count1 : 77
//count2 : 918
//count3 : 5
final var stream = LongStream.empty();

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

OptionalLong findFirst ()

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

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

final var opt = stream.findFirst();
opt.ifPresent(value -> {
    System.out.println(value); // 1
});
// parallel
final var stream = LongStream.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 = LongStream.of(1, 2, 3).unordered().parallel();

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

    final var value = opt.orElseThrow();
    if (value == 1) {
        count1++;
    } else if (value == 2) {
        count2++;
    } else if (value == 3) {
        count3++;
    }
}

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

// Result
// ↓
//count1 : 162
//count2 : 834
//count3 : 4
final var stream = LongStream.empty();

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

LongStream flatMap (LongFunction<? extends LongStream> 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 = LongStream.of(100, 200, 300);

final var mapper = new LongFunction<LongStream>() {
    @Override
    public LongStream apply(long value) {
        return LongStream.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 (LongConsumer action)

Performs an action for each element of this stream.

Please see also : forEachOrdered(LongConsumer action)

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

final var action = new LongConsumer() {
    @Override
    public void accept(long 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 = LongStream.of(-10, -20, -30);

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

// Result
// ↓
//-- forEach --
//-10
//-20
//-30
final var stream = LongStream.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 (LongConsumer 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 = LongStream.of(10, 20, 30);

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

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

// Result
// ↓
//-- forEach --
//10
//20
//30
final var stream = LongStream.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 LongStream generate (LongSupplier s)

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

final var supplier = new LongSupplier() {
    long count = 0;

    @Override
    public long getAsLong() {
        count += 10;
        return count;
    }
};

final var stream = LongStream.generate(supplier);

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

static LongStream iterate (long seed, LongPredicate hasNext, LongUnaryOperator next)

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

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

final var next = new LongUnaryOperator() {
    @Override
    public long applyAsLong(long operand) {
        return operand * 2;
    }
};

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

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

static LongStream iterate (long seed, LongUnaryOperator f)

Returns an infinite sequential ordered LongStream 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 LongUnaryOperator() {
    @Override
    public long applyAsLong(long operand) {
        return operand * 2;
    }
};

final var stream = LongStream.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));

LongStream 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 = LongStream.of(10, 20, 30, 40, 50, 60);

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

LongStream map (LongUnaryOperator mapper)

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

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

final var mapper = new LongUnaryOperator() {
    @Override
    public long applyAsLong(long operand) {
        return operand * 2;
    }
};

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

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

default LongStream mapMulti (LongStream.LongMapMultiConsumer 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 = LongStream.of(100, 200, 300);

final var mapper = new LongStream.LongMapMultiConsumer() {
    @Override
    public void accept(long value, LongConsumer lc) {
        lc.accept(value);
        lc.accept(value + 1);
        lc.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 (LongToDoubleFunction mapper)

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

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

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

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

IntStream mapToInt (LongToIntFunction mapper)

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

final var stream = LongStream.of(10000000000L, 20000000000L, 30000000000L);

final var mapper = new LongToIntFunction() {
    @Override
    public int applyAsInt(long value) {
        return Math.toIntExact(value / 1000000000);
    }
};

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

<U> Stream<U> mapToObj (LongFunction<? 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 = LongStream.of(1, 2, 3);

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

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

OptionalLong max ()

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

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

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

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

OptionalLong min ()

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

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

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

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

boolean noneMatch (LongPredicate predicate)

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

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

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

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

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

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

static LongStream of (long t)

Returns a sequential LongStream containing a single element.

final var stream = LongStream.of(0);

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

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

static LongStream of (long... values)

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

final var stream = LongStream.of(10000000000L, 20000000000L, 30000000000L);
final var ret = stream.toArray();

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

LongStream peek (LongConsumer 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 = LongStream.of(10, 20, 30);

final var action = new LongConsumer() {
    @Override
    public void accept(long 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 LongStream range (long startInclusive, long endExclusive)

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

final var stream = LongStream.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 = LongStream.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 LongStream rangeClosed (long startInclusive, long endInclusive)

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

final var stream = LongStream.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 = LongStream.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));

long reduce (long identity, LongBinaryOperator 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 = LongStream.of(1, 2, 3, 4);
final long identity = 0;

final var op = new LongBinaryOperator() {
    @Override
    public long applyAsLong(long left, long 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 = LongStream.empty();
final long identity = 0;

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

    return left + right;
});

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

OptionalLong reduce (LongBinaryOperator op)

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

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

final var op = new LongBinaryOperator() {
    @Override
    public long applyAsLong(long left, long 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 = LongStream.empty();

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

LongStream 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 = LongStream.of(10, 20, 30, 40, 50, 60, 70);

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

LongStream sorted ()

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

final var stream = LongStream.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 = LongStream.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]

long sum ()

Returns the sum of elements in this stream.

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

LongSummaryStatistics summaryStatistics ()

Returns a LongSummaryStatistics describing various summary data about the elements of this stream.

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

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

default LongStream takeWhile (LongPredicate 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 = LongStream.of(1, 2, 3, 4, 5);

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

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

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

long[] toArray ()

Returns an array containing the elements of this stream.

final var stream = LongStream.of(1, 2, 3, 4, 5);
final long[] 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