簡介
只包含一個抽象方法的接口,稱爲函數式接口;
通過Lambda
表達式來創建該接口的對象(若Lambda
表達式拋出一個受檢異常,那麼該異常需要在目標接口的抽象方法上進行聲明);
我們可以在任意函數式接口上使用@FunctionalInterface
註解,這樣做可以檢查它是否是一個函數式接口,同時javadoc
也會包含一條聲明,說明這個接口是一個函數式接口;
lambda
表達式和函數式接口是配合使用的,非函數式接口不可使用lambda
表達式;
自定義函數式接口
定義一個接口,並使用@FunctionalInterface
註解聲明:
@FunctionalInterface
public interface Calculation {
Integer cal(Integer integer);
}
使用自定義函數式接口:
// 自定義函數式接口
@Test
public void test1() {
Integer result = operation(100, (x) -> x * x);
System.out.println("自定義函數式接口:" + result);
Integer operation = operation(100, (x) -> x * 10 + 5);
System.out.println("自定義函數式接口:" + operation);
}
// 運算方法
private Integer operation(Integer integer, Calculation calculation) {
return calculation.cal(integer);
}
四大內置核心函數式接口
Consumer<T>
:消費型接口,定義一個Lambda表達式,消費(輸入)一個T類型的值,無返回值
/**
* Consumer<T>:消費型接口
*
* 定義一個Lambda表達式,消費(輸入)一個T類型的值,無返回值
*/
@Test
public void test2() {
// 調用accept方法執行lambda體
Consumer<Double> consumer = (e) -> System.out.println("消費" + e + "元");
consumer.accept(100d);
// 調用原consumer 再調用andThen方法中指定的Consumer
Consumer<Double> consumer1 = (e) -> System.out.println("消費" + (e + 10) + "元");
consumer.andThen(consumer1).accept(110d);
}
Supplier<t>
:供給型接口,定義一個Lambda表達式,無輸入,生產(返回)一個T類型的值
/**
* Supplier<t>:供給型接口
*
* 定義一個Lambda表達式,無輸入,生產(返回)一個T類型的值
*/
@Test
public void test3(){
Supplier<Integer> supplier = () -> 10;
System.out.println(supplier.get());
}
Function<T, R>
:函數型接口,定義一個Lambda表達式,輸入一個T類型的參數,返回一個R類型的值
/**
* Function<T, R>:函數型接口
* 定義一個Lambda表達式,輸入一個T類型的參數,返回一個R類型的值
*/
@Test
public void test4(){
// 傳入一個T類型參數,返回一個R類型結果
Function<Integer, Double> function = (x) -> x + 1.1;
System.out.println(function.apply(10));
System.out.println("-------------------------------");
Function<Integer, Integer> function1 = (x) -> x + 1;
//compose 先執行compose中的函數,再把返回值當作參數執行原函數
Double composeResult = function.compose(function1).apply(20);
System.out.println(composeResult); //打印:11.1 22.1
System.out.println("-------------------------------");
Function<Double, Double> function2 = (x) -> x + 1d;
//andThen 先執行原函數,再把原函數的返回值當作參數執行andThen中的函數
Double andThenResult = function.andThen(function2).apply(2);
System.out.println(andThenResult); //打印: 4.1
}
Predicate<T>
:斷言型接口,定義一個Lambda表達式,輸入一個T類型的參數,返回一個true/false
/**
* Predicate<T>:斷言型接口
* 定義一個Lambda表達式,輸入一個T類型的參數,返回一個true/false
*/
@Test
public void test5() {
// test 測試輸入的參數是否滿足定義的lambda表達式
Predicate<Integer> predicate1 = (e) -> e > 10;
System.out.println(predicate1.test(20));
System.out.println(predicate1.test(10));
System.out.println("-------------------------------");
// and 原Predicate接口和and方法中指定的Predicate接口要同時爲true,結果才爲true,同邏輯運算符&&一樣
Predicate<Integer> predicate2 = (e) -> e > 5;
System.out.println(predicate1.and(predicate2).test(9));
System.out.println(predicate1.and(predicate2).test(20));
System.out.println("-------------------------------");
//or 原Predicate接口和or方法中指定的Predicate接口有一個爲true,結果就爲true,同邏輯運算符||一樣
System.out.println(predicate1.or(predicate2).test(9));
System.out.println(predicate1.or(predicate2).test(3));
System.out.println("-------------------------------");
//negate 對結果取反再輸出
System.out.println(predicate1.negate().test(3)); // 打印:false
}