java学习笔记(10)——常用函数式接口

java.util.function

Supplier

仅含一个无参的方法,称为生产型接口,指定接口是什么泛型,get方法就返回什么类型的数据
T get():获取泛型参数指定类型的对象数据

    public static String getString(Supplier sup) {
        return sup.get();
    }

    public static void main(String[] args) {
        String s = getString(() -> {
            return "123";
        });
        System.out.println(s);
    }

案例

    public static int getMax(Supplier sup) {
        return sup.get();
    }

    public static void main(String[] args) {
        int[] arr1 = {2,323, -3434, 543, 23232, 23, -65};
        int maxValue = getMax(() -> {
            int max = arr1[0];
            for (int i : arr1) {
                if (i > max) {
                    max = i;
                }
            }
            return max;
        });

        System.out.println(maxValue);
    }

Consumer

消费一个指定泛型的数据,是一个消费型接口,
抽象:void accpet(T t)

泛型执行什么类型,就可用accept方法消费什么类型的数据

    public static void method(String name, Consumer con) {
        con.accept(name);
    }

    public static void main(String[] args) {
        method("wan", (String name) -> {
            System.out.println(name);
        });
    }

默认方法:
andThen
需要两个Consumer接口,可以把两个Consumer接口组合到一起,再对数据进行消费
java学习笔记(10)——常用函数式接口_第1张图片

    public static void method(String name, Consumer con1, Consumer con2) {
        con1.andThen(con2).accept(name);
    }

    public static void main(String[] args) {
        method("hello", (t) -> {
            System.out.println(t.toUpperCase());
        }, (t) -> {
            System.out.println(t.toLowerCase());
        });
    }
    public static void method(String[] arr, Consumer con1, Consumer con2) {
        for (String message : arr) {
            con1.andThen(con2).accept(message);
        }
    }

    public static void main(String[] args) {
        String[] arr = {"dili, girl", "zz, girl",
                "hehei, male"};

        method(arr, (message)-> {
            String name = message.split(",")[0];
            System.out.print("name:" + name);
        }, (message) -> {
            String sexy = message.split(",")[1];
            System.out.println("、性别:" + sexy + "。");
        });
    }

案例

    public static void method(String[] arr, Consumer con1, Consumer con2) {
        for (String message : arr) {
            con1.andThen(con2).accept(message);
        }
    }

    public static void main(String[] args) {
        String[] arr = {"dili, girl", "zz, girl",
                "hehei, male"};

        method(arr, (message)-> {
            String name = message.split(",")[0];
            System.out.print("name:" + name);
        }, (message) -> {
            String sexy = message.split(",")[1];
            System.out.println("、性别:" + sexy + "。");
        });
    }

和下列代码同理(上代码仅仅是用了andThen方法,有什么优点???)

    public static void method(String[] arr, Consumer con1) {
        for (String message : arr) {
            con1.accept(message);
        }
    }

    public static void main(String[] args) {

        String[] arr = {"dili, girl", "zz, girl",
                "hehei, male"};

        method(arr, (message) -> {
            String[] ms = message.split(",");
            System.out.println("姓名:" + ms[0] + "..." + "性别:" + ms[1]);
        });
    }

Predicate

对某种数据类型进行判断,返回一个boolean值

抽象方法:
boolean test(T t)

    public static boolean checkString(String s, Predicate pre) {
        return pre.test(s);
    }

    public static void main(String[] args) {
        String s = "abcde";
        //()中有没有参数,观察test方法中是否有参数
        boolean b = checkString(s, (String str) -> {
            return str.length() > 5;
        });

        System.out.println(b);
    }

默认方法:

and
    public static boolean checkString(String s, Predicate pre1, Predicate pre2) {
        return pre1.and(pre2).test(s);
    }

    public static void main(String[] args) {
        String s = "abcde12";

        boolean b = checkString(s, (String str) -> {
            return str.length() > 5;
        }, (String str) -> {
            return str.contains("a");
        });

        System.out.println(b);
    }
or
    public static boolean checkString(String s, Predicate pre1, Predicate pre2) {
        return pre1.or(pre2).test(s);
    }

    public static void main(String[] args) {
        String s = "abc2";

        boolean b = checkString(s, (String str) -> {
            return str.length() > 5;
        }, (String str) -> {
            return str.contains("a");
        });

        System.out.println(b);
    }
negate
    public static boolean checkString(String s, Predicate pre1) {
        return pre1.negate().test(s);
    }

    public static void main(String[] args) {
        String s = "abc2";

        boolean b = checkString(s, (String str) -> {
            return str.length() > 5;
        });

        System.out.println(b);
    }

案例

    public static ArrayList checkString(String[] arr, Predicate pre1, Predicate pre2) {
        ArrayList list = new ArrayList<>();

        for (String str : arr) {
            boolean b = pre1.and(pre2).test(str);
            if (b) {
                list.add(str);
            }
        }

        return list;
    }

    public static void main(String[] args) {
        String[] arr = {"wan, girl", "zzz, girl",
                "cccc, girl", "zzz, man"};

        ArrayList b = checkString(arr, (String str) -> {
            String name = str.split(", ")[0];
            return name.length() == 3;
        }, (String str) -> {
            String sexy = str.split(", ")[1];
            return sexy.equals("girl");
        });

        System.out.println(b.toString());
    }

Function

apply();
根据T的类型,将T的类型转换为R的类型
java学习笔记(10)——常用函数式接口_第2张图片

    public static void change(String s, Function fun) {
        Integer in = fun.apply(s);
        System.out.println(in);
    }

    public static void main(String[] args) {
        String s = "1234";

        change(s, (String str) -> {
            //这段代码相当于是重写apply方法
            //传递参数要看apply里包含什么,代码目的
            return Integer.parseInt(str);
        });
    }

andThen()
组合操作
java学习笔记(10)——常用函数式接口_第3张图片

    public static void change(String s, Function fun1,
                              Function fun2) {

        String stringOut = fun1.andThen(fun2).apply(s);
        System.out.println(stringOut);
    }

    public static void main(String[] args) {
        String s = "1234";

        change(s, (String str) -> {
            return Integer.parseInt(str) + 10;
        }, (Integer num) -> {
            return num + "";
        });
    }

案例

java学习笔记(10)——常用函数式接口_第4张图片

    public static int change(String s, Function fun1,
                              Function fun2, Function fun3) {
        return fun1.andThen(fun2.andThen(fun3)).apply(s);
    }

    public static void main(String[] args) {
        String s = "zz 30";

        int ss = change(s, (String str) -> {
            return str.split(" ")[1];
        }, (String str1) -> {
            return Integer.parseInt(str1);
        }, (Integer num) -> {
            return num + 100;
        });

        System.out.println(ss);
    }

你可能感兴趣的:(java)