天天看點

常見的函數式接口

/*
    在jdk8之後,多了java.util.function 。這個包下面放了很多的常用的函數式接口。

    其中有一個接口叫做Supplier<T>(生産者), 裡面有一個方法,可以擷取一個對象。

    T get​(): 可以擷取一個對象。


 */
public class Demo01Supplier {
    public static void main(String[] args) {
        method(()->"helloworld");

    }
    public static void method(Supplier<String> supplier){
        String s = supplier.get();//重寫的接口的抽象方法get。午餐數
        System.out.println(s);
    }

}
      
import java.util.function.Supplier;

//    其中有一個接口叫做Supplier<T>(生産者), 裡面有一個方法,可以擷取一個對象。
//
//            T get​(): 可以擷取一個對象。
/*
    使用函數式接口Supplier求數組元素的最大值
 */
public class Demo02SupplierTest {
    public static void main(String[] args) {
        int[] arr = new int[]{3, 4, 5, 2, 1, 10};
        getMax(() -> {
            int temp = 0;
            for (int i = 0; i < arr.length; i++) {
                if (arr[i] > temp) {
                    temp = arr[i];
                }

            }
            return temp;
        });


    }

    public static void getMax(Supplier<Integer> supplier) {
        Integer result = supplier.get();//無參數有傳回值
        System.out.println(result);
    }
}
      

---------------------------------------------------------------------------------------

*
    在java.util.function包下,還有一個函數式接口叫做Consumer,這個接口表示消費者,可以消費一個資料(使用這個資料去幹一些事情)

    Consumer<T>接口中的抽象方法:
        void accept​(T t): 消費一個資料。 接收到參數t,然後使用這個t。
 */
public class Demo02Consumer {
    public static void main(String[] args) {
//        method((b)-> System.out.println("我發現你了"));
      /*   method(new Consumer() {
            @Override
            public void accept(Object o) {
                System.out.println(o+"你好啊");

            }
         });*/

        method((t) -> System.out.println("你好啊"));//列印你好啊
        method(System.out::print);//拿到t值,轉去操作别的,不會産生引用。結果hello
        System.out.println("----------------------------");
        method((Object t) -> System.out.println(t));//hello
        method(System.out::println);//對象引用成員方法,拿到對象直接對對象進行操作//hello


    }

    public static void method(Consumer consumer) {
        consumer.accept("hello");
    }


}
      
public class Demo03Consummer {
    public static void main(String[] args) {
        method(s -> System.out.println(s.toUpperCase()),
                s -> System.out.println(s.toLowerCase()));
    }

    public static void method(Consumer<String> one, Consumer<String> two) {
        // 先調用one的accept,再調用two的accept
        //one.accept("Hello");
        //two.accept("Hello");

        // 可以把one和two合并成一個Consumer
        //Consumer three = one.andThen(two); //three是合并之後的結果,裡面包含了one的操作和two的操作
        //three.accept("Hello"); //通過three調用accept方法,因為trhee是one和two合并之後的結果。索引會先通過one調用accept,再通過two調用accept

        //最終寫法
        one.andThen(two).accept("Hello");
    }
}

      
/*
    下面的字元串數組當中存有多條資訊,請按照格式“ 姓名:XX。性别:XX。 ”的格式将資訊列印出來。

    要求将列印姓
    名的動作作為第一個 Consumer 接口的Lambda執行個體,将列印性别的動作作為第二個 Consumer 接口的Lambda實
    例,将兩個 Consumer 接口按照順序“拼接”到一起。



    使用兩個Consumer,一個列印姓名,一個列印性别。

    最終列印結果
    姓名: 迪麗熱巴  性别:女
    姓名: 古力娜紮  性别:女
    姓名: 馬爾紮哈  性别:男

 */
public class Demo04ConsumerTest {
    public static void main(String[] args) {
        String[] array = {"迪麗熱巴,女", "古力娜紮,女", "馬爾紮哈,男"};

        method(s -> System.out.print("姓名:" + s.split(",")[0]),
                s -> System.out.println(" 性别:" + s.split(",")[1]),
                array);

    }

    //定義方法,用來列印
    public static void method(Consumer<String> one, Consumer<String> two, String[] arr) {
        //周遊數組,拿到數組中的每一個元素
        for (String s : arr) {
            //列印姓名
            //one.accept(s);
            //列印性别
            //two.accept(s);
            //合并成一個Consumer
            one.andThen(two).accept(s);
        }

    }
}