函式式介面:predicate
函式描述符:t -> boolean
原始型別特化:intpredicate, longpredicate, doublepredicate
predicate介面需要實現test()方法,返回boolean型別;
boolean test(t t);
另外有三個default方法
//&&,兩個都是true才返回true
default predicateand(predicate<? super t> other)
//||,有乙個true就返回true
default predicateor(predicate<? super t> other)
// !,取反
default predicatenegate()
列印的logpredicate<
string
> predicate = s ->
"1".
equals(s) ||
"2".
equals(s);
predicate<
string
> other= s ->
"3".
equals(s) ||
"4".
equals(s);
//predicate<
string
>
and= predicate.
and(other);
predicate<
string
>
or= predicate.
or(other);
predicate<
string
> negate = predicate.negate();
system.out.println("test:"
+ predicate.test("3")+
"==and=="
+and
.test("3") +
"==or=="+or
.test("3") +
"==negate=="
+ negate.test("1"));
函式式介面:test:
false==and==false==or==true==negate==false
consumer
函式描述符:t -> void
原始型別特化:intconsumer, longconsumer, doubleconsumer
consumer介面需要實現accept()方法,沒有返回值。
void accept(t t);
乙個default方法
//先執行當前consumer物件的accept()方法,緊接著調after的accept()方法。
default
consumer
andthen(consumer
<? super
t> after) ;
}
列印logconsumer consumer = system.out::println;
consumer after = s -> system.out.println(s + "你好");
consumer.accept("hello world!");
consumer andthen = consumer.andthen(after);
andthen.accept("美女");
函式式介面:hello
world!美女
美女你好
function
函式描述符:t -> r
原始型別特化:
intfunction,
inttodoublefunction,
inttolongfunction,
longfunction,
longtodoublefunction,
longtointfunction,
doublefunction,
tointfunction,
todoublefunction,
tolongfunction
乙個default方法
default
function
compose(function
<? super
v, ? extends
t> before)
function
function = s -> s + ",你好";
function
before = s -> s + s;
function
compose = function.compose(before);
列印log
函式式介面:小子,你好
包子包子,你好
supplier
函式描述符:() -> t
原始型別特化:booleansupplier,intsupplier, longsupplier, doublesupplier
supplier只有乙個實現方法
t get();
列印logsuppliersupplier = () -> "100"
; string s = supplier.get();
system.out
.println(s);
supplierpersonsupplier = () -> ;
person person = personsupplier.get();
system.out
.println(person.name);
還有。。。100
小明
java 8 函式式介面
functionalinte ce public inte ce personsearch functionalinte ce public inte ce personsearch1 functionalinte ce public inte ce personsearch2 方法的預設實現 介面...
Java8 常用函式式介面
本文主要參考 在此感謝 接收乙個引數t,沒有返回值 原始碼 functionalinte ce public inte ce consumer 示例 講述乙個學生在學習過程中,需要買學習用品。買什麼東西是具體的實現,我們在呼叫study 方法的時候再指定。test public void testc...
java8函式式程式設計(二)
list常用操作 private static list init 迴圈列印集合中每個元素 private static void foreach private static void tolist 計算集合中age 4的元素個數 private static void filter 篩選出顏色為...