Java8常用的函式式介面

2021-09-10 13:20:58 字數 1111 閱讀 6150

1、predicate:斷言型介面

// 傳入的字串是否以 .sql 結尾

predicateisendwithsql = (s) -> s.endswith(".sql");

// 傳入的字串非 .sql 結尾

predicatenotendwithsql = isendwithsql.negate();

boolean test = isendwithsql.test("test.sql");

system.out.println(test);

boolean test1 = notendwithsql.test("test.sql");

system.out.println(test1);

// 判斷集合是否為空

predicate> isemptylist = list::isempty;

2、function:功能型介面

// 字串轉為 integer

functiontointeger = s -> integer.valueof(s); 

tointeger = integer::valueof;

function 中的 default 方法:

andthen:在 function 執行之後

compose:在 function 執行之前

3、supplier:供給型介面

suppliersbsupplier = stringbuilder::new;

stringbuilder sb = sbsupplier.get();

4、consumer:消費型介面

consumerrunnableconsumer = (run) -> new thread(run).start();

runnableconsumer.accept(() -> catch (interruptedexception e)

system.out.println("測試一下了");

});

public void test()

public void test2()

public void test3()

public void test4()

Java8 常用函式式介面

本文主要參考 在此感謝 接收乙個引數t,沒有返回值 原始碼 functionalinte ce public inte ce consumer 示例 講述乙個學生在學習過程中,需要買學習用品。買什麼東西是具體的實現,我們在呼叫study 方法的時候再指定。test public void testc...

java 8 函式式介面

functionalinte ce public inte ce personsearch functionalinte ce public inte ce personsearch1 functionalinte ce public inte ce personsearch2 方法的預設實現 介面...

Java8 中的常用函式式介面

函式式介面 predicate t 函式描述符 t boolean 原始型別特化 intpredicate,longpredicate,doublepredicate 判斷是否是正數 intpredicate predicate i i 0 true predicate.test 5 函式式介面 c...