目錄
一、介紹
二、示例
(一)Consumer?
源碼解析
測試示例?
(二)Comparator
(三)Predicate
三、應用
四、總結?
一、介紹
@FunctionalInterface是一種信息注解類型,用于指明接口類型聲明成為Java語言規范定義的函數式接口。從概念上說,函數式接口只有一個抽象方法,因為默認方法有一個實現,所以他們不是抽象的。如果一個接口聲明了一個抽象方法覆蓋java.lang的一個公共方法,這也不計入接口的抽象方法計數,因為接口的任何實現都將有來自java.lang.Object或其他地方的實現。函數式接口的實例可以使用lambda表達式、方法引用或構造函數引用來創建。
二、示例
(一)Consumer<T>?
(消費者)表示一個接受單個輸入參數并且不返回結果的操作。
源碼解析
accept()方法接收一個參數,并對該參數執行特定的操作,沒有返回值
addThen()方法接受一個consumer類型的對象,它將一個consumer對象與另一個consumer對象進行關聯,該方法會返回一個新的consumer對象,它首先執行當前consumer的accept()方法,然后再執行傳入的after consumer對象的accpet()方法。
@FunctionalInterface
public interface Consumer<T> {/*** Performs this operation on the given argument.** @param t the input argument*/void accept(T t);/*** Returns a composed {@code Consumer} that performs, in sequence, this* operation followed by the {@code after} operation. If performing either* operation throws an exception, it is relayed to the caller of the* composed operation. If performing this operation throws an exception,* the {@code after} operation will not be performed.** @param after the operation to perform after this operation* @return a composed {@code Consumer} that performs in sequence this* operation followed by the {@code after} operation* @throws NullPointerException if {@code after} is null*/default Consumer<T> andThen(Consumer<? super T> after) {Objects.requireNonNull(after);return (T t) -> { accept(t); after.accept(t); };}
}
測試示例?
public class ConsumerTest {public static void main(String[] args) {Consumer<String> printUpperCase = s -> System.out.println(s.toUpperCase());Consumer<String> printLength = s -> System.out.println(s.length());Consumer<String> combine = printUpperCase.andThen(printLength);List<String> names = Arrays.asList("Alice", "Bob", "Charlie", "David", "Jim");names.forEach(printUpperCase);names.forEach(combine);}
}
(二)Comparator<T>
(比較器)compare方法是Comparator接口中的方法,它用于比較兩個對象的大小。一般來說,如果第一個對象小于第二個對象,則返回負整數;如果第一個對象等于第二個對象,則返回零;如果第一個對象大于第二個對象,則返回正整數。
public class ComparatorTest {public static void main(String[] args) {// 自定義比較器,實現compare方法,比較規則是自然數降序排列CustomedComparator customedComparator = new CustomedComparator();List<Integer> list = Arrays.asList(5, 8, -2, 0, 10);list.sort(customedComparator);// forEach函數傳入一個consumer對象,底層是加強for循環 + 調用accpet()list.forEach(ele -> System.out.println(ele));}
}
Comparator接口聲明了函數式接口,但接口中聲明了兩個抽象方法,這顯然不符合之前給的定義。首先我先驗證是否注解允許多個抽象方法,驗證得出聲明此注解的接口只能有一個抽象方法。Comparator接口中聲明了equals和compare兩個抽象方法,?其中equals是Object類的公共方法,這里令我不解的是接口中equals方法是聲明的抽象方法,但它卻無需實現,這里需要注意一下。最后,聲明函數式接口只有一個抽象方法這是肯定的。
(三)Predicate<T>
(斷言)predicate<T>代表了一個接受一個參數并返回布爾值結果的判斷條件。該接口中只有一個抽象方法test,用于對給定的參數進行判斷,并返回一個布爾值。
public class PredicateTest {public static void main(String[] args) {Predicate<Integer> predicate = num -> num % 2 == 0;System.out.println(predicate.test(11));System.out.println(predicate.test(0));}
}
三、應用
- forEach方法,迭代器方法,參數是consumer對象。
- Arrays.sort()方法,傳入comparator對象,自定義比較
- Stream流操作
- lambda表達式
四、總結?
? 函數式接口是JDK8的新特性,在函數式接口使用ambda表達式會使代碼更加簡潔,上述內容如果有錯誤的地方,希望大佬們可以指正。我一直在學習的路上,您的幫助使我收獲更大!覺得對您有幫助的話,還請點贊支持!我也會不斷更新文章!