謂詞
接口包含以下默認和靜態(tài)方法。
我們可以使用NOT,AND和OR方法來創(chuàng)建基于其他謂詞的謂詞。
default Predicate<T> negate() default Predicate<T> and(Predicate<? super T> other) default Predicate<T> or(Predicate<? super T> other) static <T> Predicate<T> isEqual(Object targetRef)
negate()
否定原始謂詞的謂詞。
and()
組合兩個具有短路邏輯AND的謂詞。
or()
組合了具有短路邏輯或的兩個謂詞。
isEqual()
返回一個謂詞,根據(jù)Objects.equals(Object,Object)測試兩個參數(shù)是否相等。
我們可以鏈接上述方法來創(chuàng)建復雜謂詞。
以下示例顯示如何使用 Predicate
。
import java.util.function.Predicate; public class Main { public static void main(String[] args) { Predicate<String> i = (s)-> s.length() > 5; System.out.println(i.test("www.15014759268.cn ")); } }
上面的代碼生成以下結果。
更多建議: