常用的函数式接口之Predicate

public class PredicateDemo {
    public static void main(String[] args) {
        boolean b1 = checkString("hello", s -> s.length() > 8);
        System.out.println(b1);

        boolean b2 = checkString("helloWorld", s -> s.length() > 8);
        System.out.println(b2);

        boolean b3 = checkString("hello", s -> s.length() > 8,s -> s.length() < 15);
        System.out.println(b3);

        boolean b4 = checkString("helloWorld", s -> s.length() > 8,s -> s.length() < 15);
        System.out.println(b4);
        /*
            正常运算的结果: b1: false  b2: true
            逻辑非运算的结果:b1: true  b2: false
            短路与运算的结果:b3: false b4: true
            短路或运算的结果: b3: true  b4: true
         */
    }
    //同一个字符串给出两个不同的判断条件,最后把结果做逻辑与/或 作为最终结果
    private static boolean checkString(String s , Predicate<String> pre1, Predicate<String> pre2){
//        return pre1.and(pre2).test(s);
        return pre1.or(pre2).test(s);
    }
    //定义一个判断给定的字符串是否满足要求方法
    private static boolean checkString(String s , Predicate<String> pre){
        //test方法演示
//        return pre.test(s);
        //negate方法
        return pre.negate().test(s);
    }
}
posted @ 2020-06-08 15:55  硬盘红了  阅读(380)  评论(0编辑  收藏  举报