使用函數式接口
-
函數式接口定義且只定義了一個抽象方法。
-
函數式接口很有用, 因爲抽象方法的簽名可以描述Lambda表達式的簽名。
-
爲了應用不同的Lambda表達式,你需要一套能夠描述常見函數描述符的函數式接口。
-
Java 8的庫設計師幫我們在java.util.function包中引入了幾個新的函數式接口。
常用的函數式接口
一元函數
-
Function(一般函數)
-
Consumer(消費者)
-
Predicate(謂詞函數)
-
Supplier(供應者)
二元函數
-
BiFunction(一般函數)
-
BiConsumer(消費者)
-
BiPredicate(謂詞函數)
public class FunctionLearn {
/**
* Function
*/
public static void learnFunction() {
Function<String, String> functionStr = (String s) -> s + "。";
System.out.println(functionStr.apply("Hello World"));
Function<Integer, Integer> function1 = (Integer a) -> a + 2;
Integer x = function1.apply(5);
System.out.println(x);
Function<Integer, Integer> function2 = (Integer a) -> a * 2;
// 組合兩個Function函數,a * 2 compose a+2 = (a+2) * 2
Function<Integer, Integer> function3 = function2.compose(function1);
System.out.println("Function3 : " + function3.apply(20));
// 先後順序拼接兩個Function a *2 andThen a+2 = (a*2) + 2
Function<Integer, Integer> function4 = function2.andThen(function1);
System.out.println("Function4 : " + function4.apply(15));
// 輸入啥返回啥
Function.identity();
}
/**
* Consumer
*/
public static void learnConsumer() {
Consumer<Integer> consumer1 = (Integer a) -> System.out.println("Consumer 1 : " + a);
// 喫掉外部傳進來的T,在方法內部消化掉,什麼也不返回
consumer1.accept(100);
Consumer<Integer> consumer2 = (Integer a) -> System.out.println("Consumer 2 : " + a + "Done");
Consumer<Integer> consumer3 = consumer1.andThen(consumer2);
consumer3.accept(10);
consumer1.andThen(consumer2).accept(10);
}
/**
* Supplier
*/
public static void learnSupplier() {
// 無中生有,憑空生成一個東西出來
Supplier<Integer> supplier = () -> 10;
Integer a = supplier.get();
System.out.println(a);
}
/**
* Predicate
*/
public static void learnPredicate() {
Predicate<Integer> predicate1 = (Integer a) -> a > 10;
System.out.println(predicate1.test(20));
Predicate<Integer> predicate2 = (Integer a) -> a < 20;
// and 與操作
Predicate<Integer> predicate3 = predicate1.and(predicate2);
System.out.println(predicate3.test(9));
Predicate<Integer> predicate4 = (Integer a) -> a > 8;
// or 或操作
System.out.println(predicate1.or(predicate4).test(7));
// ! 取反操作
System.out.println(predicate4.negate().test(7));
}
/**
* BiFunction
*/
public static void learnBiFunction() {
BiFunction<Integer, Integer, Integer> biFunction1 = (Integer a, Integer b) -> a + b;
System.out.println(biFunction1.apply(10, 15));
Function<Integer, Integer> biFunction2 = (Integer a) -> a * 2;
System.out.println(biFunction1.andThen(biFunction2).apply(10, 15));
}
/**
* BiConsumer
*/
public static void learnBiConsumer() {
BiConsumer<Integer, Integer> biConsumer1 = (Integer a, Integer b) -> System.out.println(a + b);
biConsumer1.accept(1, 2);
BiConsumer<Integer, Integer> biConsumer2 = (Integer a, Integer b) -> System.out.println(a * b);
biConsumer1.andThen(biConsumer2).accept(1, 2);
}
/**
* BiPredicate
*/
public static void learnBiPredicate() {
BiPredicate<Integer, Integer> biPredicate1 = (Integer a, Integer b) -> a > 10 && b < 15;
System.out.println(biPredicate1.test(11, 14));
BiPredicate<Integer, Integer> biPredicate2 = (Integer a, Integer b) -> a > 13;
System.out.println(biPredicate1.and(biPredicate2).test(11, 14));
System.out.println(biPredicate1.or(biPredicate2).test(11, 14));
System.out.println(biPredicate2.negate().test(11, 14));
}
public static void main(String[] args) {
learnFunction();
learnConsumer();
learnSupplier();
learnPredicate();
learnBiFunction();
learnBiConsumer();
learnBiPredicate();
}
}
方法引用
簡介
方法引用可以被看作僅僅調用特定方法的Lambda的一種快捷寫法。
當你需要使用方法引用時,目標引用放在分隔符::前,方法的名稱放在後面。
例如, Phone::getPrice就是引用了Phone類中定義的方法getPrice。請記住,不需要括號,因爲你沒有實際調用這個方法。方法引用就是Lambda表達式(Phone a) -> a.getPrice()的快捷寫法。
構建方法引用
方法引用主要有三類。
-
指向靜態方法的方法引用(例如Integer的parseInt方法,寫作Integer::parseInt)。
-
指向任意類型實例方法的方法引用(例如String的length方法,寫作 String::length)。
-
指向現有對象的實例方法的方法引用(假設你有一個局部變量mobileCategory用於存放Category類型的對象,它支持實例方法getValue,那麼你就可以寫mobileCategory::getValue)。
釋義
第二種和第三種方法引用可能乍看起來有點兒暈。
類似於String::length的第二種方法引用的思想就是你在引用一個對象的方法,而這個對象本身是Lambda的一個參數。例如,Lambda表達式(String s) -> s.toUppeCase()可以寫作String::toUpperCase。
但第三種方法引用指的是,你在Lambda中調用一個已經存在的外部對象中的方法。例如,Lambda表達式()->mobileCategory.getValue()可以寫作mobileCategory::getValue。
public class MethodReference {
public static void main(String[] args) {
Function<String, Integer> function = (String s) -> Integer.parseInt(s);
Integer a = function.apply("15");
System.out.println(a);
// 指向靜態方法的方法引用(例如Integer的parseInt方法,寫作Integer::parseInt)。
Function<String, Integer> function1 = Integer::parseInt;
System.out.println(function1.apply("20"));
// 指向任意類型實例方法的方法引用
Function<String, Integer> function2 = (String s) -> s.length();
System.out.println(function2.apply("abc"));
Function<String, Integer> function3 = String::length;
System.out.println(function3.apply("abcd"));
// 指向現有對象的實例方法的方法引用
// 調用外部的對象
PhonePredicate phonePredicate = (Phone phone) -> true;
Function<Phone, Boolean> function4 = (Phone phone) -> phonePredicate.test(phone);
System.out.println(function4.apply(new Phone()));
Function<Phone, Boolean> function5 = phonePredicate::test;
System.out.println(function5.apply(new Phone()));
Supplier<Phone> supplier = Phone::new;
Function<Integer,Phone> function6 = Phone::new;
}
}
構造函數引用
對於一個現有構造函數,我們可以利用它的名稱和關鍵字new來創建它的一個引用: ClassName::new。
它的功能與指向靜態方法的引用類似。
無參構造函數
例如,假設有一個構造函數沒有參數。 它適合Supplier的簽名() -> Phone。
Supplier<Phone> c1 = Phone::new;
Phone a1 = c1.get();
這就等價於:
Supplier<Phone> c1 = () -> new Phone();
Phone a1 = c1.get();
有參構造函數
如果你的構造函數的簽名是Phone(Integer price),那麼它就適合Function接口的簽名。
Function<Integer, Phone> c2 = Phone::new;
Phone a2 = c2.apply(110);
這就等價於:
Function<Integer, Phone> c2 = (price) -> new Phone(price);
Phone a2 = c2.apply(110);
小結
-
Lambda表達式可以理解爲一種匿名函數:它沒有名稱,但有參數列表、函數主體、返回類型,可能還有一個可以拋出的異常的列表。
-
函數式接口就是僅僅聲明瞭一個抽象方法的接口。
-
只有在接受函數式接口的地方纔可以使用Lambda表達式。
-
Lambda表達式允許你直接內聯,爲函數式接口的抽象方法提供實現,並且將整個表達式作爲函數式接口的一個實例。
-
Java 8自帶一些常用的函數式接口,放在java.util.function包裏,包括Predicate<T>、Function<T,R>、Supplier<T>、Consumer<T>。
-
Comparator、Predicate和Function等函數式接口都有幾個可以用來結合Lambda表達式的默認方法。
作者:翎野君
博客:https://www.cnblogs.com/lingyejun/
本篇文章如有幫助到您,請給「翎野君」點個贊,感謝您的支持。