Java : LongPredicate with Examples
LongPredicate (Java SE 21 & JDK 21) with Examples.
You will find code examples on most LongPredicate methods.
Summary
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(1, 2, 3, 4);
final var ret = stream.filter(predicate).toArray();
System.out.println(Arrays.toString(ret)); // [2, 4]
// An example with a lambda expression.
final var stream = LongStream.of(1, 2, 3, 4);
final var ret = stream.filter(v -> v % 2 == 0).toArray();
System.out.println(Arrays.toString(ret)); // [2, 4]
Methods
default LongPredicate and (LongPredicate other)
final var predicate = new LongPredicate() {
@Override
public boolean test(long value) {
// Check if the number is even.
return value % 2 == 0;
}
};
System.out.println(predicate.test(1)); // false
System.out.println(predicate.test(2)); // true
System.out.println(predicate.test(13)); // false
System.out.println(predicate.test(14)); // true
final var other = new LongPredicate() {
@Override
public boolean test(long value) {
return value < 10;
}
};
System.out.println(other.test(1)); // true
System.out.println(other.test(2)); // true
System.out.println(other.test(13)); // false
System.out.println(other.test(14)); // false
final var and = predicate.and(other);
System.out.println(and.test(1)); // false
System.out.println(and.test(2)); // true
System.out.println(and.test(13)); // false
System.out.println(and.test(14)); // false
default LongPredicate negate ()
final var predicate = new LongPredicate() {
@Override
public boolean test(long value) {
// Check if the number is even.
return value % 2 == 0;
}
};
System.out.println(predicate.test(1)); // false
System.out.println(predicate.test(2)); // true
System.out.println(predicate.test(3)); // false
System.out.println(predicate.test(4)); // true
final var negate = predicate.negate();
System.out.println(negate.test(1)); // true
System.out.println(negate.test(2)); // false
System.out.println(negate.test(3)); // true
System.out.println(negate.test(4)); // false
default LongPredicate or (LongPredicate other)
final var predicate = new LongPredicate() {
@Override
public boolean test(long value) {
// Check if the number is even.
return value % 2 == 0;
}
};
System.out.println(predicate.test(1)); // false
System.out.println(predicate.test(2)); // true
System.out.println(predicate.test(13)); // false
System.out.println(predicate.test(14)); // true
final var other = new LongPredicate() {
@Override
public boolean test(long value) {
return value < 10;
}
};
System.out.println(other.test(1)); // true
System.out.println(other.test(2)); // true
System.out.println(other.test(13)); // false
System.out.println(other.test(14)); // false
final var or = predicate.or(other);
System.out.println(or.test(1)); // true
System.out.println(or.test(2)); // true
System.out.println(or.test(13)); // false
System.out.println(or.test(14)); // true
boolean test (long value)
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(1, 2, 3, 4);
final var ret = stream.filter(predicate).toArray();
System.out.println(Arrays.toString(ret)); // [2, 4]
// An example with a lambda expression.
final var stream = LongStream.of(1, 2, 3, 4);
final var ret = stream.filter(v -> v % 2 == 0).toArray();
System.out.println(Arrays.toString(ret)); // [2, 4]