一顽腾、JDK8之自定義函數(shù)式編程
1.使用Lambda表達式,自定義lambda接口編程
- 定義?個函數(shù)式接口 需要標注此接口 @FunctionalInterface,否則萬?團隊成員在接口上加了其他方法則容易出故障
- 編寫一個方法柳骄,輸入需要操做的數(shù)據(jù)和接口
- 在調(diào)用方法時傳?數(shù)據(jù) 和 lambda 表達式,用來操作數(shù)據(jù)
2.定義?個可以使用加減乘除的接口以前需要定義4個方法
- 使?Lambda表達式后
@FunctionalInterface
public interface OperFunction<R,T> {
R operator(T t1, T t2);
}
public class Main {
public static void main(String[] args) throws Exception {
System.out.println(operator(20, 5, (Integer x, Integer y) -> {
return x * y;
}));
System.out.println(operator(20, 5, (x, y) -> x + y));
System.out.println(operator(20, 5, (x, y) -> x - y));
System.out.println(operator(20, 5, (x, y) -> x / y));
}
public static Integer operator(Integer x, Integer y, OperFunction<Integer, Integer> of) {
return of.operator(x, y);
}
}
二称近、JDK8里面的函數(shù)式編程--四個功能型接口
- Lambda表達式必須先定義接口纯赎,創(chuàng)建相關(guān)方法之后才可使用,這樣做十分不便励稳,其實java8已經(jīng)內(nèi)置了許多接口, 例如下面四個功能型接口佃乘,所以?般很少會由用戶去定義新的函數(shù)式接口
- Java8的最大特性就是函數(shù)式接口,所有標注了@FunctionalInterface注解的接口都是函數(shù)式接口
Consumer<T> : 消費型接口:有入?yún)⒕阅幔瑹o返回值
void accept(T t);
Supplier<T> : 供給型接?:無入?yún)⑷け埽蟹祷刂?T get();
Function<T, R> : 函數(shù)型接口:有入?yún)ⅲ蟹祷刂?R apply(T t);
Predicate<T> : 斷?型接口:有入?yún)⑿卖幔蟹祷刂党膛粒祷刂殿愋痛_定是boolean
boolean test(T t);
三、JDK8之函數(shù)式編程 Function
- Function
- 傳入一個值經(jīng)過函數(shù)的計算返回另?個值
- T:入?yún)㈩愋偷貑琑:出參類型
- 調(diào)用方法:R apply(T t)
@FunctionalInterface
public interface Function<T, R> {
R apply(T t);
}
- 作用:將轉(zhuǎn)換邏輯提取出來愁拭,解耦合
不要看過于復雜,就是?個接口,下?是自定義實現(xiàn)
public class FunctionObj implements Function {
@Override
public Object apply(Object o) {
return o+"經(jīng)過apply處理拼接上了";
}
}
- 常規(guī)使用案例
// 輸出入?yún)⒌?0倍
Function<Integer, Integer> func = p -> p * 100;
func.apply(100);
四亏吝、JDK8之函數(shù)式編程 BiFunction
- BiFunction Function只能接收一個參數(shù)岭埠,如果要傳遞兩個參數(shù),則用 BiFunction
@FunctionalInterface
public interface BiFunction<T, U, R> {
R apply(T t, U u);
}
- 常規(guī)使用案例
public static void main(String[] args) {
System.out.println(operator(10,21,(a,b)->a+b));
System.out.println(operator(10,2,(a,b)->a-b));
System.out.println(operator(8,4,(a,b)->a*b));
System.out.println(operator(10,2,(a,b)->a/b));
}
public static Integer operator(Integer a, Integer b, BiFunction<Integer,Integer, Integer> bf) {
return bf.apply(a, b);
}
五、JDK8之函數(shù)式編程 Consumer
- Consumer
- Consumer 消費型接口:有入?yún)⑽蹬福瑹o返回值
- 將 T 作為輸?惜论,不返回任何內(nèi)容
- 調(diào)用方法:void accept(T t);
@FunctionalInterface
public interface Consumer<T> {
void accept(T t);
}
- 常規(guī)使用案例
用途:因為沒有出參,常用于打印止喷、發(fā)送短信等消費動作
public static void main(String[] args) throws Exception {
Consumer<String> consumer = obj->{
System.out.println(obj);
System.out.println("調(diào)?短信接?發(fā)送短信馆类,或者打印?志");
sendMsg("8888888",consumer);
}
public static void sendMsg(String phone,Consumer<String> consumer){
consumer.accept(phone);
}
六、JDK8之函數(shù)式編程 Supplier
- Supplier
- Supplier: 供給型接口:無入?yún)⑵羰ⅲ蟹祷刂?/li>
- T:出參類型;沒有入?yún)?/li>
- 調(diào)用方法:T get();
@FunctionalInterface
public interface Supplier<T> {
T get();
}
- 常規(guī)使用案例
用途: 泛型?定和方法的返回值類型是?種類型技羔,如果需要獲得?個數(shù)據(jù),并且不需要傳入?yún)?shù),可
以使用Supplier接?僵闯,例如無參的工廠方法,即工廠設(shè)計模式創(chuàng)建對象藤滥,簡單來說就是提供者
class Student{
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public class Test{
public static void main(String[] args) {
Student student = newStudent();
System.out.println(student.getName());
}
public static Student newStudent(){
Supplier<Student> supplier = ()-> {
Student student = new Student();
student.setName("默認名稱");
return student;
};
return supplier.get();
}
}
七鳖粟、JDK8之函數(shù)式編程 Predicate
- Predicate
- Predicate: 斷?型接口:有入?yún)ⅲ蟹祷刂底景恚祷刂殿愋痛_定是boolean
- T:入?yún)㈩愋拖蛲迹怀鰠㈩愋褪荁oolean
- 調(diào)?方法:boolean test(T t);
@FunctionalInterface
public interface Predicate<T> {
boolean test(T t);
}
- 常規(guī)使用案例
用途: 接收?個參數(shù),用于判斷是否滿足?定的條件标沪,過濾數(shù)據(jù)
public static void main(String[] args) {
List<String> list = Arrays.asList("awewrwe","vdssdsd","aoooo","psdddsd");
List<String> results = filter(list,obj->obj.startsWith("a"));
System.out.println(results);
}
public static List<String> filter(List<String> list,Predicate<String> predicate) {
List<String> results = new ArrayList<>();
for (String str : list) {
if (predicate.test(str)) {
results.add(str);
}
}
return results;
}