Java : LongPredicate con ejemplos
LongPredicate (Java SE 21 & JDK 21) en Java con ejemplos.
Encontrará ejemplos de código en la mayoría de los métodos de LongPredicate.
Nota :
- Este artículo puede utilizar software de traducción para su comodidad. Consulte también la versión original en inglés.
Summary
Representa un predicado (función con valor booleano) de un argumento con valor largo. Esta es la especialización de tipo primitivo de Predicate que lleva mucho tiempo. (Traducción automática)
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)
Devuelve un predicado compuesto que representa un AND lógico en cortocircuito de este predicado y otro. (Traducción automática)
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 ()
Devuelve un predicado que representa la negación lógica de este predicado. (Traducción automática)
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)
Devuelve un predicado compuesto que representa un O lógico en cortocircuito de este predicado y otro. (Traducción automática)
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)
Evalúa este predicado sobre el argumento dado. (Traducción automática)
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]