轉自:https://blog.csdn.net/z834410038/article/details/77370785
備註1:觀察發現,函數式接口下共有
1、三種方法
1.1 唯一的抽象方法
1.2 使用default定義普通方法(默認方法),通過對象調用。
實現接口後,因爲默認方法不是抽象方法,所以可以不重寫,但是如果開發需要,也可以重寫 。當然如果接口中的默認方法不能滿足某個實現類需要,那麼實現類可以覆蓋默認方法。簽名跟接口default方法一致,但是不能再加default修飾符。
3.使用static定義靜態方法,通過接口名調用。
2、一個新註解
如果現在某一個接口就是爲了函數式接口而生的,定義的時候就讓其只有一個抽象方法,所以有了一個新的註解:函數式接口@FunctionInterface
備註2:關於lambda表達式
JDK8以前,通過匿名內部類可以實現接口
Function<Integer, String> fun = new Function<Integer, String>() {
@Override
public String apply(Integer t) {
return String.valueOf(t);
}
};
JDK8中,通過lambda表達式實現
Function<Integer, String> fun = (x) -> String.valueOf(x);
可以得出一個結論,lambda表達式就是爲了優化匿名內部類而生。
String res = fun.apply(1000);
System.out.println(res);
lambda表達式的性能最差也就和匿名內部類一樣, 但是好的情況會更好。
一、Function功能型函數式接口
Function接口 接受一個輸入參數T,返回一個結果R。
package java.util.function;
import java.util.Objects;
@FunctionalInterface
public interface Function<T, R> {
// 接受輸入參數,對輸入執行所需操作後 返回一個結果。
R apply(T t);
// 返回一個 先執行before函數對象apply方法,再執行當前函數對象apply方法的 函數對象。
default <V> Function<V, R> compose(Function<? super V, ? extends T> before) {
Objects.requireNonNull(before);
return (V v) -> apply(before.apply(v));
}
// 返回一個 先執行當前函數對象apply方法, 再執行after函數對象apply方法的 函數對象。
default <V> Function<T, V> andThen(Function<? super R, ? extends V> after) {
Objects.requireNonNull(after);
return (T t) -> after.apply(apply(t));
}
// 返回一個執行了apply()方法之後只會返回輸入參數的函數對象。
static <T> Function<T, T> identity() {
return t -> t;
}
}
例:apply方法使用
public class FunctionDemo { static int modifyTheValue(int valueToBeOperated, Function<Integer, Integer> function) { return function.apply(valueToBeOperated); } public static void main(String[] args) { int myNumber = 10; // 使用lambda表達式實現函數式接口 // (x)->(x)+20 輸入一個參數x,進行加法運算,返回一個結果 // 所以該lambda表達式可以實現Function接口 int res1 = modifyTheValue(myNumber, (x)-> x + 20); System.out.println(res1); // 30 // 使用匿名內部類實現 int res2 = modifyTheValue(myNumber, new Function<Integer, Integer>() { @Override public Integer apply(Integer t) { return t + 20; } }); System.out.println(res2); // 30 } } 例:andThen方法使用
public static Integer modifyTheValue2(int value, Function<Integer, Integer> function1, Function<Integer, Integer> function2){ //value作爲function1的參數,返回一個結果,該結果作爲function2的參數,返回一個最終結果 return function1.andThen(function2).apply(value); } public static void main(String[] args) { System.out.println(modifyTheValue2(3, val -> val + 2, val -> val + 3)); }
二、Consumer消費型函數式接口
代表了 接受一個輸入參數並且無返回的操作
例:accept方法使用
public static void modifyTheValue3(int value, Consumer<Integer> consumer) {
consumer.accept(value);
}
public static void main(String[] args) {
// (x) -> System.out.println(x * 2)接受一個輸入參數x
// 直接輸出,並沒有返回結果
// 所以該lambda表達式可以實現Consumer接口
modifyTheValue3(3, (x) -> System.out.println(x * 2));
}
輸出:
6
三、Predicate斷言型函數式接口
接受一個輸入參數,返回一個布爾值結果。
例:test方法使用1
public static boolean predicateTest(int value, Predicate<Integer> predicate) {
return predicate.test(value);
}
public static void main(String[] args) {
// (x) -> x == 3 輸入一個參數x,進行比較操作,返回一個布爾值
// 所以該lambda表達式可以實現Predicate接口
System.out.println(predicateTest(3, (x) -> x == 3));
}
輸出:
true
例:test方法使用2
public static void eval(List<Integer> list, Predicate<Integer> predicate) {
for (Integer n : list) {
if (predicate.test(n)) {
System.out.print(n + " ");
}
}
// list.forEach(n -> {
// if (predicate.test(n)) {
// System.out.print(n + " ");
// }
// });
}
public static void main(String args[]) {
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9);
// Predicate<Integer> predicate = n -> true
// n 是一個參數傳遞到 Predicate 接口的 test 方法
// n 如果存在則 test 方法返回 true
System.out.println("輸出所有數據:");
// 傳遞參數 n
eval(list, n -> true);
// Predicate<Integer> predicate1 = n -> n%2 == 0
// n 是一個參數傳遞到 Predicate 接口的 test 方法
// 如果 n%2 爲 0 test 方法返回 true
System.out.println("\n輸出所有偶數:");
eval(list, n -> n % 2 == 0);
// Predicate<Integer> predicate2 = n -> n > 3
// n 是一個參數傳遞到 Predicate 接口的 test 方法
// 如果 n 大於 3 test 方法返回 true
System.out.println("\n輸出大於 3 的所有數字:");
eval(list, n -> n > 3);
}
輸出:
輸出所有數據:
1 2 3 4 5 6 7 8 9
輸出所有偶數:
2 4 6 8
輸出大於 3 的所有數字:
4 5 6 7 8 9
例:test方法使用3
public static boolean validInput(String name, Predicate<String> function) {
return function.test(name);
}
public static void main(String args[]) {
String name = "冷冷";
if(validInput(name, s -> !s.isEmpty() && s.length() <= 3 )) {
System.out.println("名字輸入正確");
}
}
三、Supplier供給型函數式接口
無參數,返回一個結果。
例:get方法使用
public static String supplierTest(Supplier<String> supplier) {
return supplier.get();
}
public static void main(String args[]) {
String name = "冷冷";
// () -> name.length() 無參數,返回一個結果(字符串長度)
// 所以該lambda表達式可以實現Supplier接口
System.out.println(supplierTest(() -> name.length() + ""));
}
輸出:
2
總結
其實上面的4個函數式接口,都有一個共同的特點,都使用了@FunctionInterface註解,所以這四個接口個人覺得只是按使用習慣歸類好的有代表性的函數式接口,可以直接拿來使用,其實你也可以用@FunctionInterface註解自定義想要的函數式接口,比如接收3個參數,返回1個結果。
@FunctionalInterface
public interface MyFunction {
int opThree(int a, int b, int c);
}
public class Test {
public static void main(String[] args) {
MyFunction myFunction = (a, b, c) -> a + b + c;
int result = myFunction.opThree(1, 2, 3);
System.out.println(result);
}
}
jdk1.8中,你可以看到Runnable接口也使用了@FunctionalInterface註解
@FunctionalInterface
public interface Runnable {
public abstract void run();
}
so你在創建線程的時候就可以如下
Thread thread = new Thread(() -> {
//邏輯
});