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

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

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

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

public interface DoublePredicate

方法

  • test() :此函数评估对双精度值的条件检查,并返回一个表示结果的布尔值。
boolean test(double value)
  • and() :此函数对当前对象和作为参数接收的对象应用 AND 运算,并返回新形成的谓词。此方法有一个默认实现。
default DoublePredicate and(DoublePredicate other)
  • negate() :此函数返回当前谓词的逆,即反转测试条件。此方法有一个默认实现。
default DoublePredicate negate()
  • or() :此函数对当前对象和作为参数接收的对象应用 OR 运算,并返回新形成的谓词。此方法有一个默认实现。
default DoublePredicate or(DoublePredicate other)

例子:

Java
// Java example to demonstrate DoublePredicate interface
 
import java.util.function.DoublePredicate;
 
public class DoublePredicateDemo {
    public static void main(String[] args)
    {
        // DoublePredicate to check square
        // of x is less than 100
        DoublePredicate db
            = (x) -> { return x * x < 100.0; };
        System.out.println("100 is less than 100 "
                           + db.test(10));
 
        DoublePredicate db3;
        // Test condition reversed
        db.negate();
        System.out.println("100 is greater than 100 "
                           + db.test(10));
 
        DoublePredicate db2 = (x) ->
        {
            double y = x * x;
            return y >= 36 && y < 1000;
        };
 
        // Test condition ANDed
        // with another predicate
        db3 = db.and(db2);
        System.out.println("81 is less than 100 "
                           + db3.test(9));
 
        db3 = db.or(db2);
        // Test condition ORed with another predicate
        System.out.println("49 is greater than 36"
                           + " and less than 100 "
                           + db3.test(7));
    }
}


输出:
100 is less than 100 false
100 is greater than 100 false
81 is less than 100 true
49 is greater than 36 and less than 100 true

参考: https: Java 函数