Java8-函數(shù)式接口

什么是函數(shù)式接口

? Java 8引入了函數(shù)式接口的概念

? 1). 只包含一個抽象方法的接口,稱為函數(shù)式接口

? 2). 函數(shù)式接口可以被隱式轉(zhuǎn)換為lambda表達式。

? 3). 在任意函數(shù)式接口上使用@FunctionalInterface注解,這樣做可以檢查它是否是一個函數(shù)式接口,同時javadoc 也會包含一條聲明,說明這個接口是一個函數(shù)式接口。

預定義的函數(shù)式接口

? Java 8定義了大量的預定義函數(shù)式接口,用于常見類型的代碼傳遞,這些函數(shù)定義在包java.util.function下,

其中有四大核心函數(shù)式接口。

函數(shù)式接口 參數(shù)類型 返回類型 用途
Consumer<T>(消費型接口) T void 對類型為T的對象應用操作。void accept(T t)
Supplier<T>(供給型接口) T 返回類型為T的對象。 T get();
Function<T, R>(函數(shù)型接口) T R 對類型為T的對象應用操作并返回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
        */
}

方法引用

? 當要傳遞給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;

接口中的默認方法和靜態(tài)方法

? Java8以前,接口里的方法要求全部是抽象方法,Java8以后允許在接口里定義默認方法靜態(tài)方法,默認方法使用 default 關鍵字修飾。

例如:

public interface MyFunction{
  void func();
  //聲明一個接口的默認方法
  default void testDefalut(){
    System.out.println("MyFunction 默認方法");
  }
  //聲明一個接口的靜態(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 默認方法
            MyFunction 靜態(tài)方法
  */
}

默認方法的主要優(yōu)勢是提供一種拓展接口的方法,而不破壞現(xiàn)有代碼。

接口沖突

? 如果一個父接口提供一個默認方法,而另一個接口也提供了一個具有相同名稱和參數(shù)列表的方法(不管方法是否是默認方法),那么必須覆蓋該方法來解決沖突

public interface AnotherFunction {
    default void testDefalut() {
        System.out.println("AnotherFunction 默認方法");
    }
}

public class FunctionImpl implements MyFunction,AnotherFunction{
    @Override
    public void func() {
        System.out.println(" FunctionImpl 實現(xiàn)抽象方法");
    }

    @Override
    public void testDefalut() {
         System.out.println(" FunctionImpl 覆蓋接口中默認方法解決沖突");
    }
}

? 如果不覆蓋接口中相同的默認方法,那么new MyFunctionImpl().testDefalut();中調(diào)用的testDefalut方法到底是哪個接口的testDefalut()方法呢?所以必須在實現(xiàn)類中覆蓋testDefalut()方法。

小結(jié)

? 本章中介紹了Java 8中的函數(shù)式接口,Java8四大核心函數(shù)式接口,方法的引用,接口的默認方法和靜態(tài)方法。

原文鏈接:Java8 新特性 函數(shù)式接口 | 火堯

最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

  • 標簽:java 函數(shù)式接口 Supplier<T>接口Supplier supplier=()->random....
    梓青閱讀 457評論 0 1
  • JAVA8函數(shù)式接口 函數(shù)式接口是java8的一種新特性,函數(shù)式接口定義了且只定義了一個抽象方法!該接口非常有用,...
    Snow_DZG閱讀 1,028評論 0 1
  • Java8 in action 沒有共享的可變數(shù)據(jù),將方法和函數(shù)即代碼傳遞給其他方法的能力就是我們平常所說的函數(shù)式...
    鐵牛很鐵閱讀 1,358評論 1 2
  • 前言 人生苦多,快來 Kotlin ,快速學習Kotlin! 什么是Kotlin? Kotlin 是種靜態(tài)類型編程...
    任半生囂狂閱讀 26,692評論 9 118
  • 在工廠,在研發(fā)類公司,一定有一個崗位,叫工程師。 可是什么是工程師呢?似乎業(yè)界沒有具體定義,只有期望和假想:...
    東長愨人閱讀 730評論 0 2

友情鏈接更多精彩內(nèi)容