函數式接口學習案例代碼、Lambda表達式【代碼】

1.Supplier<T>接口,有返回值的,返回值的類型與接口中的泛型一致,是一個數據的生產者
public class SupplierDemo {
    public static void main(String[] args) {
        anonymityImpl();

        //使用Lambda表達式作爲方法的參數實現
        getRes(5,()->{
            /*
                返回的也就是10 + 5 ,5以參數的方式傳遞給getRes(num,..)方法中的num
                後面是一個Lambda表達式,也是以參數的方式傳遞給此方法,這個Lambda也就是
                重寫了Supplier<>接口,直接返回了一個數字,僅此而已(此接口理解爲數據的生產者)
             */

            return 10;
        });

    }
    public static int getRes(int num,Supplier<Integer> sup)
    {
        return sup.get();
    }

    //用匿名內部類實現Supplier<T>接口
    private static void anonymityImpl() {
        System.out.println("====使用匿名內部類實現====");
        String s = new Supplier<String>() {

            @Override
            public String get() {
                return "dksnkm";
            }
        }.get();
        System.out.println(s);
    }

}


public class Supplier_getMax {
    public static void main(String[] args) {

        int arr[] = {1, 5, 3, -2, 8, -9, 10, 18};
        //使用Lambda表達式作爲方法的參數實現,重寫get()方法
        int lambdaGetMax = getMax(() -> {
            int max = arr[0];
            for (int i : arr) {
                if (max < i) {
                    max = i;
                }
            }
            return max;
        });
        int anonimityGetMax = getMaxAnonimity(arr);
        System.out.println("使用Lambda獲取到的最大值爲" + lambdaGetMax);
        System.out.println("使用匿名內部類獲取到的最大值爲" + anonimityGetMax);

    }

    public static int getMaxAnonimity(int arr[]) {
        int max = new Supplier<Integer>() {

            @Override
            public Integer get() {
                int max = arr[0];
                for (int i : arr) {
                    if (max < i) {
                        max = i;
                    }
                }
                return max;
            }
        }.get();
        return max;
    }


    public static int getMax(Supplier<Integer> sup) {
        /*
            將Lambda表達式作爲參數傳遞到此方法中,傳過來的也就是一個參數
            所以在表達式中重寫get()方法中返回的也是一個int類型的參數
         */
        return sup.get();
    }

}


2.Consumer<T>接口,是沒有返回值的,是一個消費者
public class Consumer_andThen_Demo {
    public static void main(String[] args) {

        //使用Lambda表達式作爲參數重寫接口裏面的方法,沒有返回值
        getRes("abc",(str)->{
            String s = str.toUpperCase();
            System.out.println(s);
        });

        //使用匿名內部類的方式實現此接口    實現字符串轉換爲大寫
        new Consumer<String>(){

            @Override
            public void accept(String s) {
                String s1 = s.toUpperCase();
                System.out.println(s1);
            }
        }.accept("abc");
    }

    public static void getRes(String str, Consumer<String> con){
        con.accept(str);
    }
}

public class ConcatStringArray {
    public static void main(String[] args) {

        String str[] = {"1,one", "2,two", "3,three", "4,four"};
        concatString(str, (s) -> {
                    String num = s.split(",")[0];
                    System.out.print("阿拉伯" + num);
                },
                (s) -> {
                    String numE = s.split(",")[1];
                    System.out.println(",英文數字" + numE);
                });
    }

    public static void concatString(String str[], Consumer<String> con, Consumer<String> con2) {
        for (String s : str) {
            con.andThen(con2).accept(s);
        }
    }
}
3.Predicate<T>接口,返回的是一個boolean值,有返回值的,所有在重寫方法的時候要有返回值

public class PredicateDemo01 {
    public static void main(String[] args) {

        boolean res = getRes("hjdshj",
                (str) -> {
                    return str.length() > 5;
                },
                (str) -> {
                    return str.contains("d");  
                }
        );
        System.out.println(res);

    }

    public static boolean getRes(String str, Predicate<String> pre1, Predicate<String> pre2) {
        /*
            此處使用的是and方法,也就相當於我們用的邏輯符號  && ||
            執行的步驟是pre1先執行test()方法,再就是pre2再執行
            這裏返回的順序是先pre1返回true   再pre2返回true   兩個一and(&&) 結果爲true
            注意:negate是取反
         */
        return pre1.and(pre2).test(str);
    }

}

4.Function<T,V>接口,有返回值,返回值的類型就是T
public class FunctionDemo_calculate {
    public static void main(String[] args) {

        getRes("10",
                (str) -> {
                    int i = Integer.parseInt(str);   //將字符串轉換爲int類型
                    i = i + 10;   //在原來的基礎上 +10、
                    return i;
                },
                (num) -> {
                    String str = num + "";    //再將轉換後成爲int類型的數字+10傳遞過來,重寫方法
                    return str;   //返回
                }
        );
    }

    public static void getRes(String str, Function<String, Integer> fun1, Function<Integer, String> fun2) {
       /*
            看藉口的數據類型,第一個接口初始值是String累心,最後返回的是Integer類型,第二個接口中的第一個
            數據類型是int類型(也就是一個Lambda表達式返回的int值),返回的是String類型
            題目:首先給定一個字符串,將字符串轉換爲一個int類型,並 + 10,在轉換Wie字符串輸出
        */

        String apply = fun1.andThen(fun2).apply(str);
        System.out.println(apply);

    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章