詳解如何熟練使用java函數(shù)式接口
一、函數(shù)式接口的由來
我們知道使用Lambda表達(dá)式的前提是需要有函數(shù)式接口,而Lambda表達(dá)式使用時(shí)不關(guān)心接口名,抽象方法名。只關(guān)心抽象方法的參數(shù)列表和返回值類型。因此為了讓我們使用Lambda表達(dá)式更加的方法,在JDK中提供了大量常用的函數(shù)式接口
package com.bobo.jdk.fun;
public class Demo01Fun {
public static void main(String[] args) {
fun1((arr)->{
int sum = 0 ;
for (int i : arr) {
sum += i;
}
return sum;
});
}
public static void fun1(Operator operator){
int[] arr = {1,2,3,4};
int sum = operator.getSum(arr);
System.out.println("sum = " + sum);
}
}
/**
* 函數(shù)式接口
*/
@FunctionalInterface
interface Operator{
int getSum(int[] arr);
}
二、函數(shù)式接口介紹
在JDK中幫我們提供的有函數(shù)式接口,主要是在 java.util.function 包中。
2.1 Supplier
無參有返回值的接口,對(duì)于的Lambda表達(dá)式需要提供一個(gè)返回?cái)?shù)據(jù)的類型。
@FunctionalInterface
public interface Supplier<T> {
/**
* Gets a result.
*
* @return a result
*/
T get();
}
使用:
/**
* Supplier 函數(shù)式接口的使用
*/
public class SupplierTest {
public static void main(String[] args) {
fun1(()->{
int arr[] = {22,33,55,66,44,99,10};
// 計(jì)算出數(shù)組中的最大值
Arrays.sort(arr);
return arr[arr.length-1];
});
}
private static void fun1(Supplier<Integer> supplier){
// get() 是一個(gè)無參的有返回值的 抽象方法
Integer max = supplier.get();
System.out.println("max = " + max);
}
}
2.2 Consumer
有參無返回值得接口,前面介紹的Supplier接口是用來生產(chǎn)數(shù)據(jù)的,而Consumer接口是用來消費(fèi)數(shù)據(jù)的,使用的時(shí)候需要指定一個(gè)泛型來定義參數(shù)類型
@FunctionalInterface
public interface Consumer<T> {
/**
* Performs this operation on the given argument.
*
* @param t the input argument
*/
void accept(T t);
}
使用:將輸入的數(shù)據(jù)統(tǒng)一轉(zhuǎn)換為小寫輸出
public class ConsumerTest {
public static void main(String[] args) {
test(msg -> {
System.out.println(msg + "-> 轉(zhuǎn)換為小寫:" + msg.toLowerCase());
});
}
public static void test(Consumer<String> consumer){
consumer.accept("Hello World");
}
}
默認(rèn)方法:andThen
如果一個(gè)方法的參數(shù)和返回值全部是Consumer類型,那么就可以實(shí)現(xiàn)效果,消費(fèi)一個(gè)數(shù)據(jù)的時(shí)候,首先做一個(gè)操作,然后再做一個(gè)操作,實(shí)現(xiàn)組合,而這個(gè)方法就是Consumer接口中的default方法 andThen方法
default Consumer<T> andThen(Consumer<? super T> after) {
Objects.requireNonNull(after);
return (T t) -> { accept(t); after.accept(t); };
}
具體的操作
public class ConsumerAndThenTest {
public static void main(String[] args) {
test2(msg1->{
System.out.println(msg1 + "-> 轉(zhuǎn)換為小寫:" + msg1.toLowerCase());
},msg2->{
System.out.println(msg2 + "-> 轉(zhuǎn)換為大寫:" + msg2.toUpperCase());
});
}
public static void test2(Consumer<String> c1,Consumer<String> c2){
String str = "Hello World";
//c1.accept(str); // 轉(zhuǎn)小寫
//c2.accept(str); // 轉(zhuǎn)大寫
//c1.andThen(c2).accept(str);
c2.andThen(c1).accept(str);
}
}
2.3 Function
有參有返回值的接口,F(xiàn)unction接口是根據(jù)一個(gè)類型的數(shù)據(jù)得到另一個(gè)類型的數(shù)據(jù),前者稱為前置條件,后者稱為后置條件。有參數(shù)有返回值。
@FunctionalInterface
public interface Function<T, R> {
/**
* Applies this function to the given argument.
*
* @param t the function argument
* @return the function result
*/
R apply(T t);
}
使用:傳遞進(jìn)入一個(gè)字符串返回一個(gè)數(shù)字
public class FunctionTest {
public static void main(String[] args) {
test(msg ->{
return Integer.parseInt(msg);
});
}
public static void test(Function<String,Integer> function){
Integer apply = function.apply("666");
System.out.println("apply = " + apply);
}
}
默認(rèn)方法:andThen,也是用來進(jìn)行組合操作,
default <V> Function<T, V> andThen(Function<? super R, ? extends V> after) {
Objects.requireNonNull(after);
return (T t) -> after.apply(apply(t));
}
public class FunctionAndThenTest {
public static void main(String[] args) {
test(msg ->{
return Integer.parseInt(msg);
},msg2->{
return msg2 * 10;
});
}
public static void test(Function<String,Integer> f1,Function<Integer,Integer> f2){
/*Integer i1 = f1.apply("666");
Integer i2 = f2.apply(i1);*/
Integer i2 = f1.andThen(f2).apply("666");
System.out.println("i2:" + i2);
}
}
默認(rèn)的compose方法的作用順序和andThen方法剛好相反
而靜態(tài)方法identity則是,輸入什么參數(shù)就返回什么參數(shù)
2.4 Predicate
有參且返回值為Boolean的接口
@FunctionalInterface
public interface Predicate<T> {
/**
* Evaluates this predicate on the given argument.
*
* @param t the input argument
* @return {@code true} if the input argument matches the predicate,
* otherwise {@code false}
*/
boolean test(T t);
}
使用:
public class PredicateTest {
public static void main(String[] args) {
test(msg -> {
return msg.length() > 3;
},"HelloWorld");
}
private static void test(Predicate<String> predicate,String msg){
boolean b = predicate.test(msg);
System.out.println("b:" + b);
}
}
在Predicate中的默認(rèn)方法提供了邏輯關(guān)系操作 and or negate isEquals方法
package com.bobo.jdk.fun;
import java.util.function.Predicate;
public class PredicateDefaultTest {
public static void main(String[] args) {
test(msg1 -> {
return msg1.contains("H");
},msg2 -> {
return msg2.contains("W");
});
}
private static void test(Predicate<String> p1,Predicate<String> p2){
/*boolean b1 = predicate.test(msg);
boolean b2 = predicate.test("Hello");*/
// b1 包含H b2 包含W
// p1 包含H 同時(shí) p2 包含W
boolean bb1 = p1.and(p2).test("Hello");
// p1 包含H 或者 p2 包含W
boolean bb2 = p1.or(p2).test("Hello");
// p1 不包含H
boolean bb3 = p1.negate().test("Hello");
System.out.println(bb1); // FALSE
System.out.println(bb2); // TRUE
System.out.println(bb3); // FALSE
}
}
到此這篇關(guān)于詳解如何熟練使用java函數(shù)式接口的文章就介紹到這了,更多相關(guān)java函數(shù)式接口內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java雙向循環(huán)鏈表的實(shí)現(xiàn)代碼
這篇文章介紹了java雙向循環(huán)鏈表的實(shí)現(xiàn)代碼,有需要的朋友可以參考一下2013-09-09
OpenFeign實(shí)現(xiàn)微服務(wù)間的文件下載方式
這篇文章主要介紹了OpenFeign實(shí)現(xiàn)微服務(wù)間的文件下載方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-05-05
Java實(shí)現(xiàn)Redis分布式鎖的三種方案匯總
setnx、Redisson、RedLock?都可以實(shí)現(xiàn)分布式鎖,從易到難得排序?yàn)椋簊etnx?<?Redisson?<?RedLock,本文為大家整理了三種方法的實(shí)現(xiàn),希望對(duì)大家有所幫助2023-11-11
Java concurrency線程池之線程池原理(四)_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要為大家詳細(xì)介紹了Java concurrency線程池之線程池原理,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06
監(jiān)控Spring Boot 項(xiàng)目運(yùn)行情況操作方法
在實(shí)際開發(fā)中,經(jīng)常會(huì)遇到想要獲取到服務(wù)器應(yīng)用的運(yùn)行情況的場(chǎng)景,在微服務(wù)架構(gòu)下對(duì)于每個(gè)應(yīng)用運(yùn)行情況的監(jiān)控是保證系統(tǒng)高可用的關(guān)鍵,本文給大家介紹如何實(shí)現(xiàn)在Spring Boot的jar包中對(duì)系統(tǒng)的運(yùn)行情況進(jìn)行監(jiān)控操作,感興趣的朋友跟隨小編一起看看吧2024-08-08
Java的中l(wèi)ombok下的@Builder注解用法詳解
這篇文章主要介紹了Java的中l(wèi)ombok下的@Builder注解用法詳解,lombok注解在java進(jìn)行編譯時(shí)進(jìn)行代碼的構(gòu)建,對(duì)于java對(duì)象的創(chuàng)建工作它可以更優(yōu)雅,不需要寫多余的重復(fù)的代碼,在出現(xiàn)lombok之后,對(duì)象的創(chuàng)建工作更提供Builder方法,需要的朋友可以參考下2023-11-11
idea項(xiàng)目debug模式無法啟動(dòng)的解決
這篇文章主要介紹了idea項(xiàng)目debug模式無法啟動(dòng)的解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-02-02

