1. 程式人生 > >初識Lambda表示式2(JDK提供的函式式介面的引出)----java

初識Lambda表示式2(JDK提供的函式式介面的引出)----java

一個小栗子

   為了更加深刻的理解lambda表示式,寫了如下一個栗子:

package com.nrsc.lambda.MoneyDemo;

import java.text.DecimalFormat;

@FunctionalInterface
interface FormatInterface {
    String formatNum(int i); //格式化數字的方法
}

//繼承介面的方式
class FormatClass1 implements FormatInterface {
    @Override
    public String formatNum
(int i) { DecimalFormat decimalFormat = new DecimalFormat("#,###"); String format = decimalFormat.format(i); return format; } } //繼承介面+鏈式程式設計的方式 class FormatClass2 implements FormatInterface { @Override public String formatNum(int i) { //鏈式程式設計 return
new DecimalFormat("#,###").format(i); } } class MyMoney { private int money; public MyMoney(int money) { this.money = money; } public void printMoney(FormatInterface formatInterface) { System.out.println("我的存款是:" + formatInterface.formatNum(this.money)); }
} public class MoneyDemo { public static void main(String[] args) { MyMoney myMoney = new MyMoney(8888888); System.out.println("-----繼承介面的方式-----"); FormatInterface f1 = new FormatClass1(); myMoney.printMoney(f1); System.out.println("-----繼承介面+鏈式程式設計的方式-----"); FormatInterface f2 = new FormatClass2(); myMoney.printMoney(f2); System.out.println("-----匿名內部類的方式----"); myMoney.printMoney(new FormatInterface() { @Override public String formatNum(int i) { return new DecimalFormat("#,###").format(i); } }); /** * lambda表示式1----對應於繼承介面方式 */ System.out.println("-----lambda表示式方式1-----"); myMoney.printMoney(i -> { DecimalFormat decimalFormat = new DecimalFormat("#,###"); return decimalFormat.format(i); }); /** * lambda表示式2----對應於繼承介面+鏈式程式設計的方式(如果一句話可以搞定的話,可以省去return和大括號{}) */ System.out.println("-----lambda表示式方式2-----"); myMoney.printMoney(i -> { return new DecimalFormat("#,###").format(i);}); myMoney.printMoney(i -> new DecimalFormat("#,###").format(i)); /** * 當然假如我們的lambda表示式寫成下面的樣子的話,肯定結果就是"我的存款是:8888888英鎊" */ System.out.println("======乾點別的事情======"); myMoney.printMoney(i -> i + "英鎊"); } }

   執行結果如下:
在這裡插入圖片描述
    我們可以看到在上面使用的lambda表示式其實就相當於一個匿名內部類,但與匿名內部類相比,它不用指定(或者說知道)具體介面的名稱,也不用指定(或者說知道)介面中具體要重寫的方法,只需要知道其輸入的是什麼,要輸出的是什麼就可以了-----當然還有一個比較厲害的地方是假如這個輸出用一句話就可以搞定的話,完全可以省去return和大括號{}.

函式式介面的引入

    按照上面的結論,既然lambda表示式根本不關心具體是實現的是哪個介面,即他不需要知道介面的具體名字,也不需要知道介面內具體的方法, 它只需要知道其輸入是什麼,輸出是什麼, 那我們就完全沒必要去實際地定義這樣一個介面,只要有那麼一個通用的介面,它的輸入是我們想輸入的值,它的輸出是我們想輸出的結果就可以了.-----JDK為我們提供了一些這樣的函式式介面 (以後可能會細說,下面僅僅做一個小栗子),於是上面的程式碼我們就可以用JDK給我們提供的函式式介面改寫成下面的樣子:

   請看如下程式碼:

package com.nrsc.lambda.MoneyDemo1;

import java.text.DecimalFormat;
import java.util.function.Function;

//沒必要定義該介面
@FunctionalInterface  
interface FormatInterface {
    String formatNum(int i); //格式化數字的方法 ----輸入值為int型別,輸出值型別為String
}

class MyMoney {
    private int money;

    public MyMoney(int money) {
        this.money = money;
    }

    //使用函式式介面Function<Integer, String>
    //與我們定義的FormatInterface一樣,它需要輸入值為一個Integer型別,輸出值型別為String
    public void printMoney(Function<Integer, String> formatInterface) {
        System.out.println("我的存款是:" + formatInterface.apply(this.money));
    }
}

public class MoneyDemo {
    public static void main(String[] args) {
        MyMoney myMoney = new MyMoney(8888888);

        /**
         *  可以得到與上面栗子一樣的結果
         */
        myMoney.printMoney(i -> new DecimalFormat("#,###").format(i));
    }
}

    執行結果如下:
在這裡插入圖片描述