什么是函數(shù)式接口
? Java 8引入了函數(shù)式接口的概念
? 1). 只包含一個抽象方法的接口,稱為函數(shù)式接口
? 2). 函數(shù)式接口可以被隱式轉(zhuǎn)換為lambda表達式。
? 3). 在任意函數(shù)式接口上使用@FunctionalInterface注解,這樣做可以檢查它是否是一個函數(shù)式接口丈冬,同時javadoc 也會包含一條聲明,說明這個接口是一個函數(shù)式接口炸站。
預(yù)定義的函數(shù)式接口
? Java 8定義了大量的預(yù)定義函數(shù)式接口闹伪,用于常見類型的代碼傳遞,這些函數(shù)定義在包java.util.function下届巩,
其中有四大核心函數(shù)式接口硅瞧。
函數(shù)式接口 | 參數(shù)類型 | 返回類型 | 用途 |
---|---|---|---|
Consumer<T>(消費型接口) | T | void | 對類型為T的對象應(yīng)用操作。void accept(T t) |
Supplier<T>(供給型接口) | 無 | T | 返回類型為T的對象恕汇。 T get(); |
Function<T, R>(函數(shù)型接口) | T | R | 對類型為T的對象應(yīng)用操作并返回R類型的對象腕唧。R apply(T t); |
Predicate<T>(斷言型接口) | T | boolean | 確定類型為T的對象是否滿足約束或辖。boolean test(T t); |
Consumer<T> 消費型接口
public static void consume(double money, Consumer<Double> con){
con.accept(money);
}
public static void main(String[] args) {
consume(10000, (m) -> {
System.out.println("今日全場8折");
System.out.println("顧客消費:" + (m * 0.8) + "元");
});
}
Supplier<T> 供給型接口
//生成num個整數(shù),并存入集合
public List<Integer> getNumList(int num, Supplier<Integer> sup) {
List<Integer> list = new ArrayList<>();
for (int i = 0; i < num; i++) {
Integer n = sup.get();
list.add(n);
}
return list;
}
public static void main(String[] args) {
//10個100以內(nèi)的隨機數(shù)
List<Integer> numList = getNumList(10, () -> (int) (Math.random() * 100));
for (Integer num : numList) {
System.out.println(num);
}
}
Function<T, R> 函數(shù)型接口
/*
Function接口常用于數(shù)據(jù)的處理轉(zhuǎn)換,比如給定一個員工列表,需要返回名稱列表
*/
public class Employee {
private int id;
private String name;
private double salary;
public Employee(String name){
this.name = name;
}
public Employee(String name,double salary) {
this.name = name;
this.salary = salary;
}
//省略getter setter
}
public class TestEmp{
public static <T, R>List<R> map(List<T> list,Function<T, R> fun){
List<R> returnList = new ArrayList<>(list.size());
for (T e : list) {
returnList.add(fun.apply(e));
}
return returnList
}
public static void main(String[] args) {
List<Employee> employees = Arrays.asList(new Employee("老張"),
new Employee("小李"),
new Employee("老王"),
new Employee("小劉"),
new Employee("小胖"));
List<String> nameList = map(employees, (employee -> employee.getName()));
System.out.println(nameList);
/*
console:[老張, 小李, 老王, 小劉, 小胖]
*/
}
}
Predicate<T> 斷言型接口
public static <E> List<E> filter(List<E> list, Predicate<E> pred) {
List<E> retList = new ArrayList<>();
for (E e : list) {
if (pred.test(e)) {
retList.add(e);
}
}
return retList;
}
public static void main(String[] args) {
List<Employee> employees = Arrays.asList(new Employee("老張"),
new Employee("小李", 3000.00),
new Employee("老王", 5000.00),
new Employee("小劉", 7000.00),
new Employee("小胖", 10000.00));
//過濾薪資小于5000的員工
List<Employee> filter = filter(employees,
employee -> employee.getSalary() > 5000.00);
for (Employee employee : filter) {
System.out.println(employee.getName() + ":" + employee.getSalary());
}
/*
console:小劉:7000.0
小胖:10000.0
*/
}
方法引用
? 當(dāng)要傳遞給Lambda體的操作,已經(jīng)有實現(xiàn)的方法了枣接,可以使用方法引用颂暇!方法引用:使用操作符 ::
將方法名和對象或類的名字分隔開來。如下三種主要使用情況
對象 : : 實例方法
類 : : 靜態(tài)方法
類 : : 實例方法
基本用法
例如:
//靜態(tài)方法
BinaryOperator<Double> binaryOperator = (x, y) -> Math.pow(x, y);
//等價于
BinaryOperator<Double> binaryOperator = Math::pow;
//實例方法: 類::實例方法
Function<Employee, String> f = (Employee e) -> e.getName();
//等價于
Function<Employee, String> f = Employee::getName;
//---------------------------------------------------------
//對象::實例方法
Employee e = new Employee("小李", 3000.00);
Supplier<String> s = () -> e.getName();
//等價于↓
Supplier<String> s = e::getName;
構(gòu)造方法
? 與函數(shù)式接口相結(jié)合月腋,自動與函數(shù)式接口中方法兼容蟀架。可以把構(gòu)造器引用賦值給定義的方法榆骚,與構(gòu)造器參數(shù)
列表要與接口中抽象方法的參數(shù)列表一致片拍!對于構(gòu)造方法,方法引用的語法是<類名>::new妓肢,如Employee::new
捌省,如下語句:
Function<String,Employee> f = (name)->new Employee(name);
//等價于↓
Function<String, Employee> f = Employee::new;
接口中的默認(rèn)方法和靜態(tài)方法
? Java8以前,接口里的方法要求全部是抽象方法碉钠,Java8以后允許在接口里定義默認(rèn)方法和靜態(tài)方法,默認(rèn)方法使用 default 關(guān)鍵字修飾纲缓。
例如:
public interface MyFunction{
void func();
//聲明一個接口的默認(rèn)方法
default void testDefalut(){
System.out.println("MyFunction 默認(rèn)方法");
}
//聲明一個接口的靜態(tài)方法
static void testStatic(){
System.out.println("MyFunction 靜態(tài)方法");
}
}
//MyFunctionImpl實現(xiàn)接口MyFunction
public class MyFunctionImpl implements MyFunction {
@Override
public void func() {
System.out.println("實現(xiàn)抽象方法");
}
public static void main(String[] args) {
MyFunction my = new MyFunctionImpl();
my.func();
my.testDefalut();
MyFunction.testStatic();
}
/*
實現(xiàn)抽象方法
MyFunction 默認(rèn)方法
MyFunction 靜態(tài)方法
*/
}
默認(rèn)方法的主要優(yōu)勢是提供一種拓展接口的方法,而不破壞現(xiàn)有代碼喊废。
接口沖突
? 如果一個父接口提供一個默認(rèn)方法祝高,而另一個接口也提供了一個具有相同名稱和參數(shù)列表的方法(不管方法是否是默認(rèn)方法),那么必須覆蓋該方法來解決沖突
public interface AnotherFunction {
default void testDefalut() {
System.out.println("AnotherFunction 默認(rèn)方法");
}
}
public class FunctionImpl implements MyFunction,AnotherFunction{
@Override
public void func() {
System.out.println(" FunctionImpl 實現(xiàn)抽象方法");
}
@Override
public void testDefalut() {
System.out.println(" FunctionImpl 覆蓋接口中默認(rèn)方法解決沖突");
}
}
? 如果不覆蓋接口中相同的默認(rèn)方法污筷,那么new MyFunctionImpl().testDefalut();
中調(diào)用的testDefalut方法到底是哪個接口的testDefalut()方法呢工闺?所以必須在實現(xiàn)類中覆蓋testDefalut()方法。
小結(jié)
? 本章中介紹了Java 8中的函數(shù)式接口瓣蛀,Java8四大核心函數(shù)式接口陆蟆,方法的引用,接口的默認(rèn)方法和靜態(tài)方法惋增。