LongPredicate接口被引入JDK 8。該接口封裝在java.util.function包。它對long值進行操作並根據條件返回謂詞值。它是一個函數接口因此可以用於拉姆達表達式還。
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://docs.oracle.com/javase/8/docs/api/java/util/function/LongPredicate.html
相關用法
- Java Java.util.function.DoublePredicate用法及代碼示例
- Java Java.util.function.IntPredicate用法及代碼示例
- Java Java.util.function.BiPredicate用法及代碼示例
- Java Java.util.ArrayDeque.add()用法及代碼示例
- Java Java.util.ArrayDeque.addFirst()用法及代碼示例
- Java Java.util.ArrayDeque.addLast()用法及代碼示例
- Java Java.util.ArrayDeque.clear()用法及代碼示例
- Java Java.util.ArrayDeque.clone()用法及代碼示例
- Java Java.util.ArrayDeque.descendingIterator()用法及代碼示例
- Java Java.util.ArrayDeque.element()用法及代碼示例
- Java Java.util.ArrayDeque.getFirst()用法及代碼示例
- Java Java.util.ArrayDeque.getLast()用法及代碼示例
- Java Java.util.ArrayDeque.isEmpty()用法及代碼示例
- Java Java.util.ArrayDeque.iterator()用法及代碼示例
- Java Java.util.ArrayDeque.peek()用法及代碼示例
- Java Java.util.ArrayDeque.peekFirst()用法及代碼示例
- Java Java.util.ArrayDeque.peekLast()用法及代碼示例
- Java Java.util.ArrayDeque.poll()用法及代碼示例
- Java Java.util.ArrayDeque.pollFirst()用法及代碼示例
- Java Java.util.ArrayDeque.pollLast()用法及代碼示例
- Java Java.util.ArrayDeque.pop()用法及代碼示例
- Java Java.util.ArrayDeque.push()用法及代碼示例
- Java Java.util.ArrayDeque.remove()用法及代碼示例
- Java Java.util.ArrayDeque.removeFirst()用法及代碼示例
- Java Java.util.ArrayDeque.removeLast()用法及代碼示例
注:本文由純淨天空篩選整理自CharchitKapoor大神的英文原創作品 Java.util.function.LongPredicate interface in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。