java8--- (Function、Predicate、Consumer) 通用函数式接口

//    public static void main(String[] args) throws InterruptedException {  // https://blog.csdn.net/u011848397/article/details/89074794

//        String name = "";
//        String name1 = "12345";
//
//        //3.Predicate<T>
//        String name2 = "12";
//        System.out.println(validInput(name, inputStr ->  !inputStr.isEmpty() &&  inputStr.length() <= 3 ));
//        System.out.println(validInput(name2,inputStr ->  !inputStr.isEmpty() &&  inputStr.length() <= 3 ));
//        System.out.println(validInput(name1,inputStr ->  !inputStr.isEmpty() &&  inputStr.length() <= 3 ));

//        //2.Predicate<T>
//        validInput(name, (Consumer<String>) inputStr -> System.out.println(inputStr.isEmpty() ? "名字不能为空":"名字正常"));
//        validInput(name1,(Consumer<String>) inputStr -> System.out.println(inputStr.isEmpty() ? "名字不能为空":"名字正常"));

//        //1.Function<T, R>
//        System.out.println(validInput(name,  i -> i.isEmpty()    ? "名字不能为空" : i));
//        System.out.println(validInput(name1, i -> i.length() > 3 ? "名字过长"     : i));
//    }

//    //3.Predicate<T> :将 T 作为输入,返回一个布尔值作为输出,该接口包含多种默认方法来将 Predicate 组合成其他复杂的逻辑(与、或、非)。
//    public static boolean validInput(String name, Predicate<String> function) {
//        return function.test(name);
//    }

//    //2.Consumer<T> :将 T 作为输入,不返回任何内容,表示在单个参数上的操作。
//    public static void validInput(String name, Consumer<String> function) {
//        function.accept(name);
//    }
//
//    //1.Function<T, R>:将 T 作为输入,返回 R 作为输出,还包含了和其他函数组合的默认方法。
//    public static String validInput(String name, Function<String, String> function) {
//        return function.apply(name);
//    }

猜你喜欢

转载自www.cnblogs.com/hahajava/p/12074760.html
今日推荐