常用內(nèi)置函數(shù)接口, 接口只包含一個(gè)抽象方法
- 消費(fèi)型接口:
Consumer<T> - void accept(T t)
接受參數(shù)销部,但是無返回值
class ConsumerDemo {
public static void bath(int money, Consumer<Integer> spendMoney) {
spendMoney.accept(money);
}
public static void main(String[] args) {
// 我搞了一個(gè)桃村,話費(fèi)了 = 100
bath(100,x -> System.out.println("我搞了一個(gè)套餐,話費(fèi)了=" + x));
}
}
- 供給型接口:
Supplier<T> - T get()
Supplier 接口翻譯過來就是提供者
該接口對(duì)應(yīng)的方法類型為 不接受參數(shù)
竭贩,但是提供一個(gè)返回值
使用get()方法獲得這個(gè)返回值
Supplier<String> getInstance = () -> "HelloWorld!";
System.out.println(getInstance.get());
// 控偶值臺(tái)輸出 HelloWorld
- 函數(shù)型接口:
Function<T,R> - R apply(T t)
它接受一個(gè)T
類型的參數(shù),返回一個(gè)R
類型的返回值;
通過調(diào)用apply
方法執(zhí)行內(nèi)容
public class FunctionApiExample {
/**
下面這個(gè)方法接受一個(gè)int類型參數(shù)a,返回a+1,符合接受一個(gè)參數(shù),返回一個(gè)值
所以呢這個(gè)方法就符合Function接口的定義,那要怎么用呢,繼續(xù)看例子
*/
public static int addOne(int a) {
return a+1;
}
/**
該方法第二個(gè)參數(shù)接受一個(gè)function類型的行為,然后調(diào)用apply莺禁,對(duì)a執(zhí)行這段行為
*/
public static int oper(int a, Function<Integer,Integer> action){
return action.apply(a);
}
/* 下面調(diào)用這個(gè)oper方法,將addOne方法作為參數(shù)傳遞 */
public static void main(String[] args){
//Function 單獨(dú)使用
Function<Integer,Integer> f = s->(s*2);
Integer i = f.apply(2);
System.out.println(i); //輸出4
int x = 1;
int y = oper(1,m -> addOne(m));//這里可以換成方法引用的寫法 int y = oper(x,Operation::addOne(x))
System.out.printf("x= %d, y = %d", x, y); // 打印結(jié)果 x=1, y=2
/* 當(dāng)然你也可以使用lambda表達(dá)式來表示這段行為,只要保證一個(gè)參數(shù),一個(gè)返回值就能匹配 */
y = oper(x, p -> p + 3 ); // y = 4
System.out.println("y="+y);
y = oper(x, p -> p * 3 ); // y = 3
System.out.println("y="+y);
}
}
- 斷言型接口:
Predicate<T> - boolean (T t)
中文中的‘是與
不是
是
中文語法的謂語
該接口對(duì)應(yīng)的方法為接收一個(gè)T
類型參數(shù)留量,返回一個(gè)Boolean
類型值;
多用于判斷與過濾
使用test()
方法執(zhí)行這段行為
public static void main(String[] args) {
Predicate<Integer> predOdd = integer -> integer % 2 == 1;
System.out.println(predOdd.test(5));
//控制臺(tái)輸出 true
}
方法引用
- 三種使用情況
- 對(duì)象::實(shí)例方法名(非靜態(tài)方法)
Consumer<String> con = x -> System.out.println(x);
con.accept("hello,zhangsan"); //hello,zhagnsan
/**
* 1. 對(duì)象::實(shí)例方法名(非靜態(tài)方法)
* Consumer: 方法 void accept(T t)
* 方法體中用的方法:void println(String x)
* 如果滿足上面的要求:前面的函數(shù)式接口的參數(shù)和返回值 和 具體方式體實(shí)現(xiàn)中的方法的 參數(shù) 和返回值一致哟冬,那么可以使用方法引用
*/
PrintStream ps = System.out;
Consumer<String> con2 = ps::println;
con2.accept("hello,zhangsan"); //hello,zhagnsan
- 類::靜態(tài)方法名
/**
* 2. 類::靜態(tài)方法名
* int compare(T o1, T o2);
* int compare(int x, int y);
*/
// 比較兩個(gè)數(shù)字大小
Comparator<Integer> com = (x,y) -> Integer.compare(x,y);
System.out.println(com.compare(100,200)); //-1
Comparator<Integer> co2 = Integer::compare;
System.out.println(co2.compare(1,2)); // -1
- 類::實(shí)例方法名
/**
* 3. 類::實(shí)例方法名
* 使用這個(gè)方法引用的前提是:x 作為方法的調(diào)用者楼熄,y作為方法的實(shí)際參數(shù)
*/
//比較兩個(gè)字符串是否相等
BiPredicate<String,String> bp = (x,y) -> x.equals(y);
System.out.println( bp.test("abc","abc") ); //true
BiPredicate<String,String> bp2 = String::equals;
System.out.println( bp2.test("abc","abc")); //true
Optional解決空指針異常簡單方法
方法引用
Stream 教程