詳解JAVA8 函數(shù)式接口
寫在前面
Java8中內(nèi)置了一些在開發(fā)中常用的函數(shù)式接口,極大的提高了我們的開發(fā)效率。那么,問題來了,你知道都有哪些函數(shù)式接口嗎?
函數(shù)式接口總覽
這里,我使用表格的形式來簡(jiǎn)單說明下Java8中提供的函數(shù)式接口。
四大核心函數(shù)式接口
首先,我們來看四大核心函數(shù)式接口,如下所示。
| 函數(shù)式接口 | 參數(shù)類型 | 返回類型 | 使用場(chǎng)景 |
| Consumer |
T | void | 對(duì)類型為T的對(duì)象應(yīng)用操作,接口定義的方法:void accept(T t) |
| Supplier |
無 | T | 返回類型為T的對(duì)象,接口定義的方法:T get() |
| Function<T, R>函數(shù)式接口 | T | R | 對(duì)類型為T的對(duì)象應(yīng)用操作,并R類型的返回結(jié)果。接口定義的方法:R apply(T t) |
| Predicate |
T | boolean | 確定類型為T的對(duì)象是否滿足約束條件,并返回boolean類型的數(shù)據(jù)。接口定義的方法:boolean test(T t) |
其他函數(shù)接口
除了四大核心函數(shù)接口外,Java8還提供了一些其他的函數(shù)式接口。
| 函數(shù)式接口 | 參數(shù)類型 | 返回類型 | 使用場(chǎng)景 |
| BiFunction(T, U, R) | T, U | R | 對(duì)類型為T,U的參數(shù)應(yīng)用操作,返回R類型的結(jié)果。接口定義的方法:R apply(T t, U u) |
| UnaryOperator |
T | T | 對(duì)類型為T的對(duì)象進(jìn)行一 元運(yùn)算, 并返回T類型的 結(jié)果。 包含方法為 T apply(T t) |
| BinaryOperator |
T, T | T | 對(duì)類型為T的對(duì)象進(jìn)行二 元運(yùn)算, 并返回T類型的 結(jié)果。 包含方法為 T apply(T t1, T t2) |
| BiConsumer<T, U> | T, U | void | 對(duì)類型為T, U 參數(shù)應(yīng)用 操作。 包含方法為 void accept(T t, U u) |
| ToIntFunction |
T | int | 計(jì)算int值的函數(shù) |
| ToLongFunction |
T | long | 計(jì)算long值的函數(shù) |
| ToDoubleFunction |
T | double | 計(jì)算double值的函數(shù) |
| IntFunction |
int | R | 參數(shù)為int 類型的函數(shù) |
| LongFunction |
long | R | 參數(shù)為 long類型的函數(shù) |
| DoubleFunction |
double | R | 參數(shù)為double類型的函數(shù) |
四大核心函數(shù)式接口
Consumer接口
1.接口說明
Consumer接口是消費(fèi)性接口,無返回值。Java8中對(duì)Consumer的定義如下所示。
@FunctionalInterface
public interface Consumer<T> {
void accept(T t);
default Consumer<T> andThen(Consumer<? super T> after) {
Objects.requireNonNull(after);
return (T t) -> { accept(t); after.accept(t); };
}
}
2.使用示例
public void handlerConsumer(Integer number, Consumer<Integer> consumer){
consumer.accept(number);
}
@Test
public void test1(){
this.handlerConsumer(10000, (i) -> System.out.println(i));
}
Supplier接口
1.接口說明
Supplier接口是供給型接口,有返回值,Java8中對(duì)Supplier接口的定義如下所示。
@FunctionalInterface
public interface Supplier<T> {
T get();
}
2.使用示例
public List<Integer> getNumberList(int num, Supplier<Integer> supplier){
List<Integer> list = new ArrayList<>();
for(int i = 0; i < num; i++){
list.add(supplier.get())
}
return list;
}
@Test
public void test2(){
List<Integer> numberList = this.getNumberList(10, () -> new Random().nextInt(100));
numberList.stream().forEach(System.out::println);
}
Function接口
1.接口說明
Function接口是函數(shù)型接口,有返回值,Java8中對(duì)Function接口的定義如下所示。
@FunctionalInterface
public interface Function<T, R> {
R apply(T t);
default <V> Function<V, R> compose(Function<? super V, ? extends T> before) {
Objects.requireNonNull(before);
return (V v) -> apply(before.apply(v));
}
default <V> Function<T, V> andThen(Function<? super R, ? extends V> after) {
Objects.requireNonNull(after);
return (T t) -> after.apply(apply(t));
}
static <T> Function<T, T> identity() {
return t -> t;
}
}
2.使用示例
public String handlerString(String str, Function<String, String> func){
return func.apply(str);
}
@Test
public void test3(){
String str = this.handlerString("binghe", (s) -> s.toUpperCase());
System.out.println(str);
}
Predicate接口
1.接口說明
Predicate接口是斷言型接口,返回值類型為boolean,Java8中對(duì)Predicate接口的定義如下所示。
@FunctionalInterface
public interface Predicate<T> {
boolean test(T t);
default Predicate<T> and(Predicate<? super T> other) {
Objects.requireNonNull(other);
return (t) -> test(t) && other.test(t);
}
default Predicate<T> negate() {
return (t) -> !test(t);
}
default Predicate<T> or(Predicate<? super T> other) {
Objects.requireNonNull(other);
return (t) -> test(t) || other.test(t);
}
static <T> Predicate<T> isEqual(Object targetRef) {
return (null == targetRef)
? Objects::isNull
: object -> targetRef.equals(object);
}
}
2.使用示例
public List<String> filterString(List<String> list, Predicate<String> predicate){
List<String> strList = new ArrayList<>();
for(String str : list){
if(predicate.test(str)){
strList.add(str);
}
}
return strList;
}
@Test
public void test4(){
List<String> list = Arrays.asList("Hello", "Lambda", "binghe", "lyz", "World");
List<String> strList = this.filterString(list, (s) -> s.length() >= 5);
strList.stream().forEach(System.out::println);
}
注意:只要我們學(xué)會(huì)了Java8中四大核心函數(shù)式接口的用法,其他函數(shù)式接口我們也就知道如何使用了!
寫在最后
最后,附上Java8新特性核心知識(shí)圖,祝大家在學(xué)習(xí)Java8新特性時(shí)少走彎路。

以上就是詳解JAVA8 函數(shù)式接口的詳細(xì)內(nèi)容,更多關(guān)于JAVA8 函數(shù)式接口的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
解讀為什么@Autowired在屬性上被警告,在setter方法上不被警告問題
在Spring開發(fā)中,@Autowired注解常用于實(shí)現(xiàn)依賴注入,它可以應(yīng)用于類的屬性、構(gòu)造器或setter方法上,然而,當(dāng)@Autowired注解在屬性上使用時(shí),IntelliJIDEA等IDE會(huì)給出Fieldinjectionisnotrecommended的警告,而在setter方法上使用@Autowired時(shí)卻不會(huì)出現(xiàn)這個(gè)警告2025-02-02
常用的Spring Boot調(diào)用外部接口方式實(shí)現(xiàn)數(shù)據(jù)交互
Spring Boot提供了多種調(diào)用外部接口的方式,可以方便地實(shí)現(xiàn)與其他系統(tǒng)的數(shù)據(jù)交互,提高系統(tǒng)的可擴(kuò)展性和數(shù)據(jù)共享能力,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2023-04-04
從最基本的Java工程搭建SpringMVC+SpringDataJPA+Hibernate
本文會(huì)介紹從一個(gè)最基本的java工程,到Web工程,到集成Spring、SpringMVC、SpringDataJPA+Hibernate,本文介紹的非常詳細(xì),具有參考借鑒價(jià)值,感興趣的朋友一起學(xué)習(xí)吧2016-05-05
Springboot-注解-操作日志的實(shí)現(xiàn)方式
這篇文章主要介紹了Springboot-注解-操作日志的實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-03-03
如何對(duì)Mysql數(shù)據(jù)表查詢出來的結(jié)果進(jìn)行排序
這篇文章主要介紹了如何對(duì)Mysql數(shù)據(jù)表查詢出來的結(jié)果進(jìn)行排序問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-08-08
mybatis?mapper.xml中如何根據(jù)數(shù)據(jù)庫類型選擇對(duì)應(yīng)SQL語句
這篇文章主要介紹了mybatis?mapper.xml中如何根據(jù)數(shù)據(jù)庫類型選擇對(duì)應(yīng)SQL語句,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-01-01

