Java 8 Predicate 函数接口

开发 后端
Predicate 函数接口同之前介绍的 Function 接口一样,是一个函数式接口,它可以接受一个泛型 <T> 参数,返回值为布尔类型。

[[414975]]

本文转载自微信公众号「未读代码」,作者未读君。转载本文请联系未读代码公众号。

Predicate 函数接口同之前介绍的 Function 接口一样,是一个函数式接口,它可以接受一个泛型 <T> 参数,返回值为布尔类型。Predicate 常用于数据过滤,如过滤出集合中符合某个条件的元素。

源码:Java 8 中函数接口 Predicate。

  1. package java.util.function
  2.  
  3. import java.util.Objects; 
  4.  
  5. @FunctionalInterface 
  6. public interface Predicate<T> { 
  7.  
  8.     boolean test(T t); 
  9.  
  10.     default Predicate<T> and(Predicate<? super T> other) { 
  11.         Objects.requireNonNull(other); 
  12.         return (t) -> test(t) && other.test(t); 
  13.     } 
  14.    
  15.     default Predicate<T> negate() { 
  16.         return (t) -> !test(t); 
  17.     } 
  18.  
  19.     default Predicate<T> or(Predicate<? super T> other) { 
  20.         Objects.requireNonNull(other); 
  21.         return (t) -> test(t) || other.test(t); 
  22.     } 
  23.  
  24.     static <T> Predicate<T> isEqual(Object targetRef) { 
  25.         return (null == targetRef) 
  26.                 ? Objects::isNull 
  27.                 : object -> targetRef.equals(object); 
  28.     } 

1. Predicate test

Predicate 函数接口可以用于判断一个参数是否符合某个条件。

示例:判断某个字符串是否为空。

  1. import java.util.function.Predicate; 
  2.  
  3. public class Java8PredicateTest { 
  4.     public static void main(String[] args) { 
  5.         Predicate<String> isEmpty = String::isEmpty; 
  6.         System.out.println(isEmpty.test("")); 
  7.         System.out.println(isEmpty.test("www.wdbyte.com")); 
  8.     } 

输出结果:

  1. true 
  2. false 

2. Predicate Stream filter

Stream 中的 filter() 方法是通过接收一个 Predicate 函数接口实现的。

示例:过滤出集合中,字符串长度为 4 的字符串。

  1. import java.util.Arrays; 
  2. import java.util.List; 
  3. import java.util.stream.Collectors; 
  4.  
  5. public class Java8PredicateFilter { 
  6.  
  7.     public static void main(String[] args) { 
  8.         List<String> list = Arrays.asList("java""node""www.wdbyte.com"); 
  9.         list = list.stream().filter(str -> str.length() == 4).collect(Collectors.toList()); 
  10.         System.out.println(list); 
  11.     } 

输出结果:

  1. [java, node] 

3. Predicate and

使用 and() 方法,可以让前后两个 Predicate 判断条件一起生效。

示例 1:过滤数字集合中,数字大小在 5 至 9 之间的数字。

  1. import java.util.Arrays; 
  2. import java.util.List; 
  3. import java.util.function.Predicate; 
  4. import java.util.stream.Collectors; 
  5.  
  6. public class Java8PredicateAnd { 
  7.  
  8.     public static void main(String[] args) { 
  9.         List<Integer> numberList = Arrays.asList(3, 4, 5, 6, 7, 8, 9, 10); 
  10.  
  11.         Predicate<Integer> greaterThan5 = number -> number > 5; 
  12.         Predicate<Integer> lessThan9 = number -> number < 9; 
  13.         Predicate<Integer> filter = greaterThan5.and(lessThan9); 
  14.  
  15.         numberList = numberList.stream().filter(filter).collect(Collectors.toList()); 
  16.         System.out.println(numberList); 
  17.     } 

结果输出:

  1. [6, 7, 8] 

示例 2:一个 Predicate 过滤数字集合中,数字大小在 5 至 9 之间的数字。

  1. List<Integer> numberList = Arrays.asList(3, 4, 5, 6, 7, 8, 9, 10); 
  2. numberList = numberList.stream().filter(x -> x > 5 && x < 9).collect(Collectors.toList()); 
  3. System.out.println(numberList); 

输出结果;

  1. [6, 7, 8] 

4. Predicate negate

predicate.negate() 方法会返回一个与指定判断相反的 Predicate。

示例:过滤数字集合中,数字不大于 5 的数字。

  1. import java.util.Arrays; 
  2. import java.util.List; 
  3. import java.util.function.Predicate; 
  4. import java.util.stream.Collectors; 
  5.  
  6. public class Java8PredicateNeagete { 
  7.  
  8.     public static void main(String[] args) { 
  9.         List<Integer> numberList = Arrays.asList(3, 4, 5, 6, 7, 8, 9, 10); 
  10.         Predicate<Integer> greaterThan5 = number -> number > 5; 
  11.  
  12.         numberList = numberList.stream().filter(greaterThan5.negate()).collect(Collectors.toList()); 
  13.         System.out.println(numberList); 
  14.     } 

输出结果:

  1. [3, 4, 5] 

5. Predicate or

示例:过滤数字集合中,数字小于等于 5,或者大于等于 9 的数字。

  1. import java.util.Arrays; 
  2. import java.util.List; 
  3. import java.util.function.Predicate; 
  4. import java.util.stream.Collectors; 
  5.  
  6. public class Java8PredicateOr { 
  7.  
  8.     public static void main(String[] args) { 
  9.         List<Integer> numberList = Arrays.asList(3, 4, 5, 6, 7, 8, 9, 10); 
  10.  
  11.         Predicate<Integer> lessThan5 = number -> number <= 5; 
  12.         Predicate<Integer> greaterThan8 = number -> number >= 9; 
  13.  
  14.         numberList = numberList.stream().filter(lessThan5.or(greaterThan8)).collect(Collectors.toList()); 
  15.         System.out.println(numberList); 
  16.     } 

输出结果:

  1. [3, 4, 5, 9, 10] 

6. Predicate 链式编程

Predicate 的 or() ,and(),negate() 方法可以随意组合 Predicate,组合后的判断逻辑是从左到右,从前到后,顺次判断。

如:(数字小于 5 ).and (数字大于 9 ).negate()。

解:(数字小于 5 )AND (数字大于 9 ) 对于任意数字都得 false,false.negate() 取相反 得 true。

所以,此判断逻辑对于任意数字都为 true。

示例:Predicate 的 or() ,and(),negate() 方法组合使用。

  1. import java.util.ArrayList; 
  2. import java.util.Arrays; 
  3. import java.util.List; 
  4. import java.util.function.Predicate; 
  5.  
  6. public class Java8PredicateChain { 
  7.  
  8.     public static void main(String[] args) { 
  9.         List<Integer> numberList = Arrays.asList(3, 4, 5, 6, 7, 8, 9, 10); 
  10.  
  11.         Predicate<Integer> lessThan5 = number -> number <= 5; 
  12.         Predicate<Integer> greaterThan9 = number -> number >= 9; 
  13.  
  14.         // 小于等于 5 
  15.         System.out.println(filter(numberList, lessThan5)); 
  16.         // 大于 5 
  17.         System.out.println(filter(numberList, lessThan5.negate())); 
  18.         // 小于等于 5 或者大于等于 9 
  19.         System.out.println(filter(numberList, lessThan5.or(greaterThan9))); 
  20.         // ! (小于等于 5 AND 大于等于 9) 
  21.         System.out.println(filter(numberList, lessThan5.and(greaterThan9).negate())); 
  22.     } 
  23.  
  24.     public static <T> List<T> filter(List<T> list, Predicate<T> predicate) { 
  25.         List<T> resultList = new ArrayList<>(); 
  26.         for (T t : list) { 
  27.             if (predicate.test(t)) { 
  28.                 resultList.add(t); 
  29.             } 
  30.         } 
  31.         return resultList; 
  32.     } 

输出结果:

  1. [3, 4, 5] 
  2. [6, 7, 8, 9, 10] 
  3. [3, 4, 5, 9, 10] 
  4. [3, 4, 5, 6, 7, 8, 9, 10] 

7. Predicate 与对象

示例:过滤符合某些特征的狗。

  1. import java.util.ArrayList; 
  2. import java.util.List; 
  3. import java.util.function.Predicate; 
  4.  
  5. public class Java8PredicateObject { 
  6.  
  7.     public static void main(String[] args) { 
  8.         List<Dog> dogList = new ArrayList<>(); 
  9.         dogList.add(new Dog("哈士奇", 1)); 
  10.         dogList.add(new Dog("牧羊犬", 2)); 
  11.         dogList.add(new Dog("柯基", 3)); 
  12.         dogList.add(new Dog("柴犬", 3)); 
  13.  
  14.         // 找到 3岁的狗 
  15.         System.out.println(filter(dogList, dog -> dog.getAge().equals(3))); 
  16.         // 找到哈士奇信息 
  17.         Predicate<Dog> predicate = dog -> ("哈士奇").equals(dog.getName()); 
  18.         System.out.println(filter(dogList, predicate)); 
  19.     } 
  20.  
  21.     public static <T> List<T> filter(List<T> list, Predicate<T> predicate) { 
  22.         List<T> resultList = new ArrayList<>(); 
  23.         for (T t : list) { 
  24.             if (predicate.test(t)) { resultList.add(t); } 
  25.         } 
  26.         return resultList; 
  27.     } 
  28.  
  29. class Dog { 
  30.     private String name
  31.     private Integer age; 
  32.  
  33.     public Dog(String nameInteger age) { 
  34.         this.name = name
  35.         this.age = age; 
  36.     } 
  37.  
  38.     public String getName() { 
  39.         return name
  40.     } 
  41.  
  42.     public void setName(String name) { 
  43.         this.name = name
  44.     } 
  45.  
  46.     public Integer getAge() { 
  47.         return age; 
  48.     } 
  49.  
  50.     public void setAge(Integer age) { 
  51.         this.age = age; 
  52.     } 
  53.  
  54.     @Override 
  55.     public String toString() { 
  56.         return "Dog{" + 
  57.             "name='" + name + '\'' + 
  58.             ", age=" + age + 
  59.             '}'
  60.     } 

输出结果:

  1. [Dog{name='柯基', age=3}, Dog{name='柴犬', age=3}] 
  2. [Dog{name='哈士奇', age=1}] 

BiPredicate 和 Predicate 函数接口一样,都是返回一个布尔类型,唯一不同的是 Predicate 接受一个参数,而 BiPredicate 可以接受两个不同类型的参数。

BiPredicate 在 Java 8 中源码:

  1. package java.util.function
  2.  
  3. import java.util.Objects; 
  4. @FunctionalInterface 
  5. public interface BiPredicate<T, U> { 
  6.     boolean test(T t, U u); 
  7.  
  8.     default BiPredicate<T, U> and(BiPredicate<? super T, ? super U> other) { 
  9.         Objects.requireNonNull(other); 
  10.         return (T t, U u) -> test(t, u) && other.test(t, u); 
  11.     } 
  12.  
  13.     default BiPredicate<T, U> negate() { 
  14.         return (T t, U u) -> !test(t, u); 
  15.     } 
  16.  
  17.     default BiPredicate<T, U> or(BiPredicate<? super T, ? super U> other) { 
  18.         Objects.requireNonNull(other); 
  19.         return (T t, U u) -> test(t, u) || other.test(t, u); 
  20.     } 

 

责任编辑:武晓燕 来源: 未读代码
相关推荐

2021-04-14 07:33:02

Java函数式断言

2023-07-26 07:13:55

函数接口Java 8

2019-08-05 08:05:27

Java开发代码

2020-05-25 16:25:17

Java8Stream函数式接口

2023-10-23 14:16:01

Java函数式编程

2024-02-28 08:37:28

Lambda表达式Java函数式接口

2020-10-16 10:07:03

Lambda表达式Java8

2022-12-26 07:47:37

JDK8函数式接口

2023-12-22 16:39:47

Java函数式接口开发

2015-12-21 11:47:22

Java 8组合函数

2022-01-19 11:19:25

Java/接口/代码

2015-09-28 14:54:08

Java函数式编程

2015-09-30 09:34:09

java8字母序列

2022-11-30 16:58:01

Java系统函数式

2014-01-02 15:30:56

PostgreSQLJava

2009-06-22 14:28:00

java接口

2020-10-19 13:03:16

Java 8接口抽象类

2020-10-14 10:22:14

Java 8 接口特性

2022-01-06 07:39:17

Java Default关键字 Java 基础

2021-11-30 08:44:29

SpringRouter Func函数式接口
点赞
收藏

51CTO技术栈公众号