• JDK8-Predicate接口使用举例


             目录

    Predicate接口的作用

    Predicate接口:判断相等isEqual

    Predicate接口:评估字符串

    Predicate接口:评估Collection集合

    Predicate接口:与或非逻辑判断

    情景1:过滤掉成绩为空,或者姓名为空、或者id为空的学生

    情景2:过滤掉成绩为空,且姓名为空、且id为空的学生

    情景3:获取成绩为空,或者姓名为空、或者id为空的学生


    Predicate接口的作用

            Java 8引入了Predicate函数式接口,主要用于表示一个参数的谓词(布尔值函数),在功能上类似于JavaScript-Array的filter()方法。

            关于“谓词”解释,百度给出了如下解释:

             此处,Predicate接口主要是:结合自身的default默认方法——与and()、或or()、非negate(),static静态方法isEqual(),返回一个Predicate对象;然后通过重写内部的test()抽象方法,返回一个布尔值

              这个布尔值就可以用于表示被描述的对象是否与指定的条件相符合(true),或者不符合(false)

    Predicate接口:判断相等isEqual

             Predicate接口提供了静态成员方法isEqual(),用于判断两个对象是否相等。一个经典应用场景是:结合Stream流式操作,根据指定元素的值,筛选集合中与之相同的所有元素、计算集合中与之相同的元素个数

            例如:我们想筛选集合Collection中,与给定元素相等的元素及其个数,示例代码如下,

            PS:注意TStudent类需要重写equals和hasCode()方法。

    1. public static void main(String[] args) {
    2. Collection collection1 = new ArrayList<>();
    3. collection1.add(new TStudent(1, "Tom", 3.8));
    4. collection1.add(new TStudent(2, null, 3.5));
    5. collection1.add(new TStudent(3, "John", 2.3));
    6. collection1.add(new TStudent(4, "Mike", null));
    7. collection1.add(new TStudent(null, "Yoke", 4.5));
    8. collection1.add(new TStudent(2, null, 3.5));
    9. collection1.add(new TStudent());
    10. System.out.println("原始集合:");
    11. collection1.forEach(System.out::println);
    12. TStudent temp = new TStudent(2, null, 3.5);
    13. Predicate predicate = Predicate.isEqual(temp);
    14. System.out.println("【筛选统计结果】");
    15. //1-获取集合中等于temp的元素
    16. Stream tStudentStream = collection1.stream().filter(predicate);
    17. List collect = tStudentStream.collect(Collectors.toList());//Stream转List
    18. collect.forEach(System.out::println);
    19. //2-统计集合中等于temp的元素个数
    20. long count = collection1.stream().filter(predicate).count();
    21. System.out.println("个数:"+count);
    22. }

    Predicate接口:评估字符串

            Predicate接口可以接收一个泛型类型,此处,我们可以接收一个String字符串,借助一个Predicate接口实例,来评估字符串是否满足指定规则。

            定义规则为:字符串的长度大于5,且包含任意英文字母时,即为通过测试。

            示例代码如下,

    1. //methods
    2. public static void main(String[] args) {
    3. //利用Predicate接口判断String字符串是否满足指定规则
    4. Predicate predicate = new Predicate() {
    5. @Override
    6. public boolean test(String s) {
    7. //判断:[1]长度是否大于5;[2]是否包含英文字母
    8. return s.length()>5&&s.matches(".*[a-zA-Z].*");
    9. }
    10. };
    11. boolean test1 = predicate.test("123a45");
    12. System.out.println(test1);
    13. boolean test2 = predicate.test("659748");
    14. System.out.println(test2);
    15. }

    Predicate接口:评估Collection集合

            Predicate接口也可以与Collection集合对象结合,用于实现集合元素的过滤操作。

            例如,指定规则为:删除Collection集合对象中所有的奇数。示例代码如下,

    1. public static void main(String[] args) {
    2. //定义集合对象
    3. Collection collection = new ArrayList();
    4. collection.add(1);
    5. collection.add(2);
    6. collection.add(3);
    7. collection.add(4);
    8. collection.add(5);
    9. boolean flag = collection.removeIf(new Predicate() {
    10. @Override
    11. public boolean test(Object o) {
    12. Integer value = Integer.parseInt(o.toString());
    13. return value % 2 != 0;
    14. }
    15. });
    16. System.out.println("flag="+flag);
    17. collection.forEach(System.out::println);
    18. }

             更复杂一点的应用场景,我们可以借助Predicate来过滤掉绩点成绩为空的学生元素。示例代码如下,

    1. //methods
    2. public static void main(String[] args) {
    3. //定义集合对象
    4. Collection collection1 = new ArrayList<>();
    5. collection1.add(new TStudent(1,"Tom",null));
    6. collection1.add(new TStudent(2,"Linda",null));
    7. collection1.add(new TStudent(3,"John",2.3));
    8. collection1.add(new TStudent(4,"Mike",null));
    9. collection1.add(new TStudent(5,"Yoke",4.5));
    10. //Predicate过滤掉绩点成绩为空的学生元素
    11. boolean flag1 = collection1.removeIf(new Predicate() {
    12. @Override
    13. public boolean test(TStudent tStudent) {
    14. return Objects.isNull(tStudent) || Objects.isNull(tStudent.getGrade());
    15. }
    16. });
    17. System.out.println("flag1="+flag1);
    18. collection1.forEach(System.out::println);
    19. }

     Predicate接口:与或非逻辑判断

            以上示例仅仅是使用了Predicate接口的test()方法。

            Predicate接口也提供了三个默认方法and()、or()、negate(),用于执行与或非逻辑判断,但是如何使用呢?

            以下,我们创建三个Predicate对象,分别用于判断:

    【1】Predicate-1:判断学生成绩是否为空;

    【2】Predicate-2:判断学生姓名是否为空;

    【3】Predicate-3:判断学生的Id是否为空。

            然后我们执行以下几类操作,

    【1】过滤掉成绩为空,或者姓名为空、或者id为空的学生;

    【2】过滤掉成绩为空,且姓名为空、且id为空的学生;

    【3】获取成绩为空,或者姓名为空、或者id为空的学生;

            示例代码如下,

    情景1:过滤掉成绩为空,或者姓名为空、或者id为空的学生

    1. public static void main(String[] args) {
    2. Collection collection1 = new ArrayList<>();
    3. collection1.add(new TStudent(1, "Tom", 3.8));
    4. collection1.add(new TStudent(2, null, 3.5));
    5. collection1.add(new TStudent(3, "John", 2.3));
    6. collection1.add(new TStudent(4, "Mike", null));
    7. collection1.add(new TStudent(null, "Yoke", 4.5));
    8. System.out.println("过滤前:");
    9. collection1.forEach(System.out::println);
    10. //Predicate-判断学生id为空
    11. Predicate predicate_id = new Predicate() {
    12. @Override
    13. public boolean test(TStudent tStudent) {
    14. return Objects.isNull(tStudent)||Objects.isNull(tStudent.getId());
    15. }
    16. };
    17. //Predicate-判断学生姓名为空
    18. Predicate predicate_name = new Predicate() {
    19. @Override
    20. public boolean test(TStudent tStudent) {
    21. return Objects.isNull(tStudent)||Objects.isNull(tStudent.getName());
    22. }
    23. };
    24. //Predicate-判断学生成绩为空
    25. Predicate predicate_grade = new Predicate() {
    26. @Override
    27. public boolean test(TStudent tStudent) {
    28. return Objects.isNull(tStudent) || Objects.isNull(tStudent.getGrade());
    29. }
    30. };
    31. System.out.println("过滤后:");
    32. //[1] 过滤掉成绩为空,或者姓名为空、或者id为空的学生;
    33. boolean flag_1 = collection1.removeIf(predicate_id.or(predicate_name).or(predicate_grade));
    34. collection1.forEach(System.out::println);
    35. }

     情景2:过滤掉成绩为空,且姓名为空、且id为空的学生

    1. public static void main(String[] args) {
    2. Collection collection1 = new ArrayList<>();
    3. collection1.add(new TStudent(1, "Tom", 3.8));
    4. collection1.add(new TStudent(2, null, 3.5));
    5. collection1.add(new TStudent(3, "John", 2.3));
    6. collection1.add(new TStudent(4, "Mike", null));
    7. collection1.add(new TStudent(null, "Yoke", 4.5));
    8. collection1.add(new TStudent());
    9. System.out.println("过滤前:");
    10. collection1.forEach(System.out::println);
    11. //Predicate-判断学生id为空
    12. Predicate predicate_id = new Predicate() {
    13. @Override
    14. public boolean test(TStudent tStudent) {
    15. return Objects.isNull(tStudent)||Objects.isNull(tStudent.getId());
    16. }
    17. };
    18. //Predicate-判断学生姓名为空
    19. Predicate predicate_name = new Predicate() {
    20. @Override
    21. public boolean test(TStudent tStudent) {
    22. return Objects.isNull(tStudent)||Objects.isNull(tStudent.getName());
    23. }
    24. };
    25. //Predicate-判断学生成绩为空
    26. Predicate predicate_grade = new Predicate() {
    27. @Override
    28. public boolean test(TStudent tStudent) {
    29. return Objects.isNull(tStudent) || Objects.isNull(tStudent.getGrade());
    30. }
    31. };
    32. System.out.println("过滤后:");
    33. //[2] 过滤掉成绩为空,且姓名为空、且id为空的学生
    34. boolean flag_2 = collection1.removeIf(predicate_id.and(predicate_name).and(predicate_grade));
    35. collection1.forEach(System.out::println);
    36. }

      情景3:获取成绩为空,或者姓名为空、或者id为空的学生

    1. public static void main(String[] args) {
    2. Collection collection1 = new ArrayList<>();
    3. collection1.add(new TStudent(1, "Tom", 3.8));
    4. collection1.add(new TStudent(2, null, 3.5));
    5. collection1.add(new TStudent(3, "John", 2.3));
    6. collection1.add(new TStudent(4, "Mike", null));
    7. collection1.add(new TStudent(null, "Yoke", 4.5));
    8. collection1.add(new TStudent());
    9. System.out.println("过滤前:");
    10. collection1.forEach(System.out::println);
    11. //Predicate-判断学生id为空
    12. Predicate predicate_id = new Predicate() {
    13. @Override
    14. public boolean test(TStudent tStudent) {
    15. return Objects.isNull(tStudent)||Objects.isNull(tStudent.getId());
    16. }
    17. };
    18. //Predicate-判断学生姓名为空
    19. Predicate predicate_name = new Predicate() {
    20. @Override
    21. public boolean test(TStudent tStudent) {
    22. return Objects.isNull(tStudent)||Objects.isNull(tStudent.getName());
    23. }
    24. };
    25. //Predicate-判断学生成绩为空
    26. Predicate predicate_grade = new Predicate() {
    27. @Override
    28. public boolean test(TStudent tStudent) {
    29. return Objects.isNull(tStudent) || Objects.isNull(tStudent.getGrade());
    30. }
    31. };
    32. System.out.println("过滤后:");
    33. //[3] 获取成绩为空,或者姓名为空、或者id为空的学生-[即:过滤掉ID、name、grade都不为空的元素]
    34. boolean flag_3 = collection1.removeIf(predicate_id.negate().and(predicate_name.negate()).and(predicate_grade.negate()));
    35. collection1.forEach(System.out::println);
    36. }

  • 相关阅读:
    尺度空间和使用各向异性扩散进行边缘检测——Scale-Space and Edge Detection Using Anisotropic Diffusion
    自己编写神经网络
    Tensorboard安装及简单使用
    8/12 最小表示法+牛客月赛
    verilog实现AM调制及仿真验证
    E. Gardener and Tree(拓扑排序)
    Centos下安装FTP并进行虚拟用户访问方式配置
    cesium-Web网页优化进阶
    免杀技术(详细)
    Kafka(一)使用Docker Compose安装单机Kafka以及Kafka UI
  • 原文地址:https://blog.csdn.net/weixin_43524214/article/details/127780711