关于java:java学习笔记10常用函数式接口

33次阅读

共计 3828 个字符,预计需要花费 10 分钟才能阅读完成。

java.util.function

Supplier<T>

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

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

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

案例

    public static int getMax(Supplier<Integer> 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<T>

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

泛型执行什么类型,就可用 accept 办法生产什么类型的数据

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

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

默认办法:
andThen
须要两个 Consumer 接口,能够把两个 Consumer 接口组合到一起,再对数据进行生产

    public static void method(String name, Consumer<String> con1, Consumer<String> 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<String> con1, Consumer<String> 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<String> con1, Consumer<String> 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<String> 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<T>

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

形象办法:
boolean test(T t)

    public static boolean checkString(String s, Predicate<String> 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<String> pre1, Predicate<String> 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<String> pre1, Predicate<String> 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<String> 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<String> checkString(String[] arr, Predicate<String> pre1, Predicate<String> pre2) {ArrayList<String> 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<String> 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());
    }

正文完
 0