Java : IntPredicate con ejemplos
IntPredicate (Java SE 21 & JDK 21) en Java con ejemplos.
Encontrará ejemplos de código en la mayoría de los métodos de IntPredicate.
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 int. Esta es la especialización de tipo primitivo de Predicate que consume int. (Traducción automática)
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(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 = IntStream.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 IntPredicate and (IntPredicate 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 IntPredicate() {
@Override
public boolean test(int 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 IntPredicate() {
@Override
public boolean test(int 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 IntPredicate negate ()
Devuelve un predicado que representa la negación lógica de este predicado. (Traducción automática)
final var predicate = new IntPredicate() {
@Override
public boolean test(int 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 IntPredicate or (IntPredicate 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 IntPredicate() {
@Override
public boolean test(int 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 IntPredicate() {
@Override
public boolean test(int 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 (int value)
Evalúa este predicado sobre el argumento dado. (Traducción automática)
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(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 = IntStream.of(1, 2, 3, 4);
final var ret = stream.filter(v -> v % 2 == 0).toArray();
System.out.println(Arrays.toString(ret)); // [2, 4]