目錄
函數式接口:Functional Interface
-
定義:
就是一個有且僅有一個抽象方法,但是可以有多個非抽象方法的接口
-
JDK 1.8之前已有的函數式接口:
java.lang.Runnable
java.util.concurrent.Callable
java.security.PrivilegedAction
java.util.Comparator
java.io.FileFilter
java.nio.file.PathMatcher
java.lang.reflect.InvocationHandler
java.beans.PropertyChangeListener
java.awt.event.ActionListener
javax.swing.event.ChangeListener
-
JDK 1.8 新增加的函數接口:
java.util.function
常用函數式接口:
Predicate<T> | 接收T對象並返回boolean |
BiConsumer<T, U> | 接收T、U對象,無返回值 |
Consumer<T> | 接收T對象,無返回值 |
Comparator<T> | 接收T對象 返回int |
Function<T, R> | 接收T對象,返回R對象 |
Supplier<T> | 提供T對象(例如工廠),不接收值 |
UnaryOperator<T> | 接收T對象,返回T對象 |
BinaryOperator<T> | 接收兩個T對象,返回T對象 |
常用函數式接口示例:
package com.java.demo;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
import com.java.domain.User;
/**
* 常用函數式接口示例
* @author: li
* @date: 2018年7月27日
*/
public class FIDemo {
public static void main(String[] args) {
//Predicate<T> 接收T對象並返回boolean
Predicate<String> predicate = (s) -> s.length()>0;
//返回結果爲boolean類型
boolean b = predicate.test("hello");
System.out.println(b);//結果:true
String s = "test";
Predicate<String> isEmpty = String::isEmpty;
System.out.println(isEmpty.test(s));//結果:false
//Function<T, R> 接收T對象,返回R對象
Function<String, Integer> toInteger = Integer::valueOf;
//先執行toInteger 然後執行String::valueOf(即先轉爲Integer 然後轉爲String)
Function<String, String> backToString = toInteger.andThen(String::valueOf);
//返回結果爲R類型
String str = backToString.apply("123");
System.out.println(str); //結果:123
//Supplier<T> 提供T對象,不接收參數
Supplier<User> personSupplier = User::new;//方法引用 User構造器
User user = personSupplier.get(); // new User
System.out.println(user); //結果:User [id=0, age=0, name=null]
//Consumer<T> 接收T對象,無返回值
List<String> list = Arrays.asList("peter", "anna", "mike", "xenia");
Consumer<String> consumer = name -> System.out.print(name+"==");
//Lambda表達式對list集合進行遍歷
list.forEach(consumer); //結果:peter==anna==mike==xenia==
//Comparator<T> 接收T對象 返回int
List<String> names = Arrays.asList("jack", "mary", "tom", "bruce");
Comparator<String> comparator = (m,n) -> n.compareTo(m);
//Lambda表達式對list集合進行排序
Collections.sort(names, comparator);
//遍歷list集合
for (String name : names) { //結果tom mary jack bruce
System.out.print(name+" ");
}
//BiConsumer<T, U> 接收T、U對象,無返回值
Map<Integer, String> map = new HashMap<Integer,String>();
map.put(1, "peter");
map.put(2, "anna");
map.put(3, "mike");
map.put(4, "xenia");
BiConsumer<Integer,String> biConsumer = (k,v) ->{
System.out.print("key:" + k + " ");
System.out.print("value:" + v + "===");
//結果:key:1 value:peter===key:2 value:anna===key:3 value:mike===key:4 value:xenia===
};
//Lambda表達式對map集合進行遍歷
map.forEach(biConsumer);
}
}
自定義函數式接口:
-
定義自定義函數式接口:
package com.java.demo;
/**
* 自定義函數式接口
* @author: li
* @date: 2018年7月27日
*/
@FunctionalInterface
public interface MyFI<T> {
void test(T t);
}
-
User實體類:
package com.java.domain;
public class User {
private int id;
private int age;
private String name;
public User() {
super();
}
public User(int id, int age, String name) {
super();
this.id = id;
this.age = age;
this.name = name;
}
//省略Getters/Setters/toString...
}
-
使用自定義函數式接口:
package com.java.demo;
import java.util.function.Supplier;
import com.java.domain.User;
/**
* 自定義函數式接口使用
* @author: li
* @date: 2018年7月27日
*/
public class diyFI {
public static void main(String[] args) {
MyFI<User> fi = u -> System.out.println("user's age is: " + u.getAge());
Supplier<User> personSupplier = User::new;
User user = personSupplier.get();
user.setAge(5);
testMyFI(user, fi);//結果:user's age is: 5
}
public static void testMyFI(User user,MyFI<User> myFI) {
myFI.test(user);
}
}