📜  Java .util。 Java中的函数.IntPredicate接口与示例

📅  最后修改于: 2022-05-13 01:55:10.361000             🧑  作者: Mango

Java .util。 Java中的函数.IntPredicate接口与示例

IntPredicate接口是在JDK 8中引入的。该接口封装在Java.util 中。函数包。它对整数值进行操作并根据条件返回谓词值。它是一个函数式接口,因此也可以在 lambda 表达式中使用。

public interface IntPredicate

方法:

  1. test() :此函数评估对 int 值的条件检查,并返回一个表示结果的布尔值。
boolean test(int value)

        2. and() :此函数对当前对象和作为参数接收的对象应用 AND 运算,并返回新形成的谓词。此方法有一个默认实现。

default IntPredicate and(IntPredicate other)

        3. negate() :此函数返回当前谓词的逆,即反转测试条件。此方法有一个默认实现。

default IntPredicate negate()

         4. or() :此函数对当前对象和作为参数接收的对象应用 OR 运算,并返回新形成的谓词。此方法有一个默认实现。

default IntPredicate or(IntPredicate other)

例子:

Java
// Java example to demonstrate IntPredicate interface
 
import java.util.function.IntPredicate;
 
public class IntPredicateDemo {
    public static void main(String[] args)
    {
        // Predicate to check a value is less than 544331
        IntPredicate intPredicate = (x) ->
        {
            if (x <= 544331)
                return true;
            return false;
        };
 
        System.out.println("544331 is less than 544331 "
                           + intPredicate.test(544331));
 
        // Predicate to check a value is equal to 544331
        IntPredicate predicate = (x) ->
        {
            if (x == 544331)
                return true;
            return false;
        };
 
        System.out.println("544331 is equal to 544331 "
                           + predicate.test(544331));
 
        // ORing the two predicates
        IntPredicate intPredicate1 = intPredicate.or(predicate);
        System.out.println("544331 is less than equal to 544331 "
                           + intPredicate1.test(544331));
 
        // ANDing the two predicates
        intPredicate1 = intPredicate.and(predicate);
        System.out.println("544331 is equal to 544331 "
                           + intPredicate1.test(544331));
 
        // Negating the predicate
        intPredicate1 = intPredicate.negate();
        System.out.println("544331 is greater than 544331 "
                           + intPredicate1.test(544331));
    }
}


输出:
544331 is less than 544331 true
544331 is equal to 544331 true
544331 is less than equal to 544331 true
544331 is equal to 544331 true
544331 is greater than 544331 false

参考: https: Java 函数