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

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

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

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

public interface LongPredicate

方法

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

例子:

Java
// Java example to demonstrate LongPredicate interface
 
import java.util.function.LongPredicate;
 
public class LongPredicateDemo {
    public static void main(String[] args)
    {
        // Predicate to check for equal to 500000
        LongPredicate longPredicate = (x) ->
        {
            return (x == 500000);
        };
        System.out.println("499999 is equal to 500000 "
                           + longPredicate.test(499999));
 
        // Predicate to check for less than equal to 500000
        LongPredicate longPredicate1 = (x) ->
        {
            return (x <= 500000);
        };
        System.out.println("499999 is less than equal to 500000 "
                           + longPredicate1.test(499999));
 
        // ANDing the two predicates
        LongPredicate longPredicate2
            = longPredicate.and(longPredicate1);
        System.out.println("500000 is equal to 500000 "
                           + longPredicate2.test(500000));
 
        // ORing the two predicates
        longPredicate2 = longPredicate.or(longPredicate1);
        System.out.println("500001 is less than equal to 500000 "
                           + longPredicate2.test(500001));
 
        // Negating the predicate
        longPredicate2 = longPredicate1.negate();
        System.out.println("499999 is greater than 500000 "
                           + longPredicate2.test(499999));
    }
}


输出:
499999 is equal to 500000 false
499999 is less than equal to 500000 true
500000 is equal to 500000 true
500001 is less than equal to 500000 false
499999 is greater than 500000 false

参考: https: Java 函数