Lambda中Function函数接口(四)——Predicate系列接口

函数式接口汇总链接

Predicate系列函数接口表示接收一个任意类型的参数,但是只会返回boolean类型的结果值。

 

  1. Predicate

    源码如下,可以看到该函数接口中有5个方法,其中 一个是静态方法。
    @FunctionalInterface
    public interface Predicate<T> {
    
        /**
         * 计算给定的参数
         */
        boolean test(T t);
    
        /**
         * 且的关系,相当于逻辑运算中 && 的作用。计算两个Predicate对象结果是否都为true 。
         */
        default Predicate<T> and(Predicate<? super T> other) {
            Objects.requireNonNull(other);
            return (t) -> test(t) && other.test(t);
        }
    
        /**
         * 取反,相当于 ! 的作用。返回给定参数计算后的值再取反。
         */
        default Predicate<T> negate() {
            return (t) -> !test(t);
        }
    
        /**
         * 或的关系,相当于逻辑运算中 || 的作用,计算两个Predicate对象执行后的值,有一个为true结果便为true 。
         */
        default Predicate<T> or(Predicate<? super T> other) {
            Objects.requireNonNull(other);
            return (t) -> test(t) || other.test(t);
        }
    
        /**
         * 注意:这个是静态方法,其实就是Object类中的equal()方法
         */
        static <T> Predicate<T> isEqual(Object targetRef) {
            return (null == targetRef)
                    ? Objects::isNull
                    : object -> targetRef.equals(object);
        }
    }

    示例代码如下:

    public class TestPredicate {
    
        public static void main(String[] args) {
            test(100, val -> val > 50);  // 结果:true
    
            testNegate(100, v -> v > 50);  // 结果:false
    
            testOr(100, v -> v > 200, o -> o > 300);  // 结果:false
            testOr(100, v -> v > 200, o -> o > 50);  // 结果:true
            testOr(100, v -> v > 50, o -> o > 300);  // 结果:true
    
            testAnd("test", str -> str.equals("test"), o -> o.length() == 4);  // 结果:true
            testAnd("test", str -> str.equals("test"), o -> o.length() == 8);  // 结果:false
            testAnd("test", str -> str.equals("testtest"), o -> o.length() == 4);  // 结果:false
    
            testEqual("test", "test");  // 结果:true
            testEqual("test", "testtest");  // 结果:false
            testEqual("testtest", "test");  // 结果:false
        }
    
        public static void test(int val, Predicate<Integer> predicate) {
            boolean test = predicate.test(val);
            System.out.println(test);
        }
    
        public static void testNegate(int val, Predicate<Integer> predicate) {
            Predicate<Integer> negate = predicate.negate();
            boolean test = negate.test(val);
            System.out.println(test);
        }
    
        public static void testOr(int val, Predicate<Integer> predicate, Predicate<Integer> other) {
            boolean test = predicate.or(other).test(val);
            System.out.println(test);
        }
    
        public static void testAnd(String str, Predicate<String> predicate, Predicate<String> other) {
            boolean test = predicate.and(other).test(str);
            System.out.println(test);
        }
    
        public static void testEqual(String s1, String s2) {
            Predicate<Object> equal = Predicate.isEqual(s1);
            boolean test = equal.test(s2);
            System.out.println(test);
        }
    }

     

  2. BiPredicate

    Bipredicate是接收两个参数的接口,和Predicate相比少了一个静态方法isEqual()。其余用法都一样。看一下源码
    @FunctionalInterface
    public interface BiPredicate<T, U> {
    
        boolean test(T t, U u);
    
        default BiPredicate<T, U> and(BiPredicate<? super T, ? super U> other) {
            Objects.requireNonNull(other);
            return (T t, U u) -> test(t, u) && other.test(t, u);
        }
    
        default BiPredicate<T, U> negate() {
            return (T t, U u) -> !test(t, u);
        }
    
        default BiPredicate<T, U> or(BiPredicate<? super T, ? super U> other) {
            Objects.requireNonNull(other);
            return (T t, U u) -> test(t, u) || other.test(t, u);
        }
    }

    其实还是那四个方法,具体使用如下:

    public class TestBiPredicate {
        public static void main(String[] args) {
    
            test(100, 200, (v1, v2) -> v1 > v2);  // 结果:false
    
            testOr("test", "testtest", (s1, s2) -> s2.length() == s1.length(), (o1, o2) -> o1.equals(o2));  // 结果:false
    
            testOr("test", "testtest", (s1, s2) -> s2.length() == 4, (o1, o2) -> "testtest".equals(o2));  // 结果:true
            testOr("test", "testtest", (s1, s2) -> s2.length() == 4, (o1, o2) -> "test".equals(o2));  // 结果:false
            testOr("test", "testtest", (s1, s2) -> s2.length() == 8, (o1, o2) -> "testtest".equals(o2));  // 结果:true
    
            testAnd("test", "test", (s1, s2) -> s2.length() == s2.length(), (o1, o2) -> o1.equals(o2));  // 结果:true
    
        }
    
        public static void test(int v1, int v2, BiPredicate<Integer, Integer> predicate) {
            boolean test = predicate.test(v1, v2);
            System.out.println(test);
        }
    
        public static void testOr(String s1, String s2, BiPredicate<String, String> predicate, BiPredicate<String, String> other) {
            BiPredicate<String, String> or = predicate.or(other);
            boolean test = or.test(s1, s2);
            System.out.println(test);
        }
    
        public static void testAnd(String s1, String s2, BiPredicate<String, String> predicate, BiPredicate<String, String> other) {
            BiPredicate<String, String> and = predicate.and(other);
            boolean test = and.test(s1, s2);
            System.out.println(test);
        }
    }

     

  3. IntPredicate

    IntPredicate也是有四个方法,test() 、 or()、  and()、 negate(),只处理给定的一个Int类型的参数,并返回boolean类型的值,就补举例了,参考Predicate的用法。
  4. LongPredicate

    同IntPredicate一样,包含四个方法,只接受Long类型的参数,参考Predicate的用法。
  5. DoublePredicate

    同IntPredicate一样,包含四个方法,只接受Double类型的参数,参考Predicate的用法。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值