当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java Java.util.function.DoublePredicate用法及代码示例


DoublePredicate接口被引入JDK 8。该接口封装在java.util.function包。它对 Double 对象进行操作并根据条件返回谓词值。它是一个函数接口因此可以用于拉姆达表达式还。

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://docs.oracle.com/javase/8/docs/api/java/util/function/DoublePredicate.html



相关用法


注:本文由纯净天空筛选整理自CharchitKapoor大神的英文原创作品 Java.util.function.DoublePredicate interface in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。