• Java 常用函数式接口 —— Predicate接口


    JDK提供了大量常用的函数式接口以丰富Lambda的典型使用场景,它们主要在 java.util.function 包中被提供。 下面是最简单的Predicate接口及使用示例。

    Predicate接口概述

    有时候我们需要对某种类型的数据进行判断,从而得到一个boolean值结果。这时可以使用java.util.function.Predicate<T> 接口。

     

    抽象方法:test

    Predicate 接口中包含一个抽象方法: boolean test(T t) 。用于条件判断的场景:

    import java.util.function.Predicate;
    
    public class Demo01Predicate {
        public static void main(String[] args) {
            method(s -> s.length() > 5);
        }
    
        private static void method(Predicate<String> predicate) {
            boolean veryLong = predicate.test("HelloWorld");
            System.out.println("字符串很长吗:" + veryLong);
        }
    }

    运行程序,控制台输出:

    字符串很长吗:true

    条件判断的标准是传入的Lambda表达式逻辑,只要字符串长度大于5则认为很长。

    默认方法:and

    既然是条件判断,就会存在与、或、非三种常见的逻辑关系。其中将两个 Predicate 条件使用“与”逻辑连接起来实 现“并且”的效果时,可以使用default方法 and 。其JDK源码为:

    default Predicate<T> and(Predicate<? super T> other) {
        Objects.requireNonNull(other);
        return (t) -> test(t) && other.test(t);
    }

    如果要判断一个字符串既要包含大写“H”,又要包含大写“W”,那么:

    import java.util.function.Predicate;
    
    public class DemoPredicateAnd {
        public static void main(String[] args) {
            boolean isValid = method(
                    // String.contains()方法,仅当此字符串包含指定的字符值序列时返回true。
                    s -> s.contains("H"),
                    s -> s.contains("W")
            );
            System.out.println("字符串符合要求吗:" + isValid);
        }
    
        private static boolean method(Predicate<String> one, Predicate<String> two) {
            boolean isValid = one.and(two).test("Hello world");
            return isValid;
        }
    }

    运行程序,控制台输出:

    字符串符合要求吗:false

    默认方法:or

    and 的“与”类似,默认方法 or 实现逻辑关系中的“或”。JDK源码为:

    default Predicate<T> or(Predicate<? super T> other) {
        Objects.requireNonNull(other);
        return (t) -> test(t) || other.test(t);
    }

    如果希望实现逻辑“字符串包含大写“H”或者包含大写“W”,那么代码只需要将“and”修改为“or”名称即可,其他都不变:

    import java.util.function.Predicate;
    
    public class DemoPredicateOr {
        public static void main(String[] args) {
            boolean isValid = method(
                    // String.contains()方法,仅当此字符串包含指定的字符值序列时返回true。
                    s -> s.contains("H"),
                    s -> s.contains("W")
            );
            System.out.println("字符串符合要求吗:" + isValid);
        }
    
        private static boolean method(Predicate<String> one, Predicate<String> two) {
            boolean isValid = one.or(two).test("Hello world");
            return isValid;
        }
    }

    运行程序,控制台输出:

    字符串符合要求吗:true

    默认方法:negate

    “与”、“或”已经了解了,剩下的“非”(取反)也会简单。默认方法 negate 的JDK源代码为:

    default Predicate<T> negate() {
        return (t) -> !test(t);
    }

    从实现中很容易看出,它是执行了test方法之后,对结果boolean值进行“!”取反而已。一定要在 test 方法调用之前

    调用 negate 方法,正如 and 和 or 方法一样:

    import java.util.function.Predicate;
    
    public class DemoPredicateNegate {
        public static void main(String[] args) {
            method(s -> s.length() > 5);
        }
    
        private static void method(Predicate<String> predicate) {
            boolean veryLong = predicate.negate().test("HelloWorld");
            System.out.println("字符串很长吗:" + veryLong);
        }
    }

    运行程序,控制台输出:

    字符串很长吗:false

    练习:集合信息筛选

    题目

    数组当中有多条“姓名+性别”的信息如下,请通过 Predicate 接口的拼装将符合要求的字符串筛选到集合 ArrayList 中,需要同时满足两个条件:

    1. 必须为女生;

    2. 姓名为4个字。

    String[] array = { "迪丽热巴,女", "古力娜扎,女", "马尔扎哈,男", "赵丽颖,女" };

    解答

    import java.util.ArrayList;
    import java.util.List;
    import java.util.function.Predicate;
    
    public class DemoPredicate {
        public static void main(String[] args) {
            String[] array = { "迪丽热巴,女", "古力娜扎,女", "马尔扎哈,男", "赵丽颖,女" };
            List<String> list = filter(
                    array,
                    s -> "女".equals(s.split(",")[1]),
                    s -> s.split(",")[0].length() == 4
            );
            System.out.println(list);
        }
    
        private static List<String> filter(String[] array, Predicate<String> one, Predicate<String> two) {
            List<String> list = new ArrayList<>();
            for (String info : array) {
                if (one.and(two).test(info)) {
                    list.add(info);
                }
            }
            return list;
        }
    }

              

  • 相关阅读:
    Python 描述符(descriptor) 杂记
    Celery 使用简介
    异步任务神器 Celery 简明笔记
    高性能框架gevent和gunicorn在web上的应用及性能测试
    Flask + Gunicorn + Nginx 部署
    Mysql查看最大连接数和修改最大连接数
    配置 influxDB 鉴权及 HTTP API 写数据的方法
    Java 字符串拼接 五种方法的性能比较分析 从执行100次到90万次
    linux端口开放指定端口的两种方法
    java自带的监控工具VisualVM一
  • 原文地址:https://www.cnblogs.com/liyihua/p/12286097.html
Copyright © 2020-2023  润新知