描述:Predicate< T>接口接受一個T類型參數,返回一個boolean值。
源碼:
public interface Predicate<T> {
boolean test(T t);
}
- 1
- 2
- 3
測試代碼如下:
Predicate<Integer> boolValue = x -> x>5;//1.
log.info(boolValue.test(1) + "");//2.
log.info(boolValue.test(6) + "");//3.
- 1
- 2
- 3
1表示定義了一個Predicate實現,入參爲Integer,返回傳入參數與5的比較。
2,3表示調用1的實現,傳入相關參數,下面看結果:
十二月 14, 2016 11:41:43 下午 com.zm.demo.PredicateTest testPre
信息: false
十二月 14, 2016 11:41:43 下午 com.zm.demo.PredicateTest testPre
信息: true
- 1
- 2
- 3
- 4
第一個輸出爲false, 第二個輸出爲true,結果與 。預期一樣。
當然了,這裏入參也可以是其他的,比如字符串,對字符串進行比較啥的,這些看官們可以自己去實現了。