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
相關用法
- Java Java.util.function.LongPredicate用法及代碼示例
- 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.DoublePredicate interface in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。