Java : LongStream with Examples
LongStream (Java SE 21 & JDK 21) with Examples.
You will find code examples on most LongStream methods.
Summary
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)
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)
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 ()
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 ()
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 ()
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 ()
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)
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)
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 ()
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 ()
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)
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 ()
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)
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 ()
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 ()
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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 ()
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 ()
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)
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)
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)
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)
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)
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)
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)
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)
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)
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 ()
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 ()
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 ()
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)
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 ()
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
- API Examples