上一篇学习了下Function接口的使用,本篇我们学习下另一个实用的函数式接口Predicate。

Predicate的源码跟Function的很像,我们可以对比这两个来分析下。直接上Predicate的源码:

public interface Predicate<T> {
/**
* Evaluates this predicate on the given argument.
*/
boolean test(T t); /**
* Returns a composed predicate that represents a short-circuiting logical
* AND of this predicate and another. When evaluating the composed
* predicate, if this predicate is {@code false}, then the {@code other}
* predicate is not evaluated.
*/
default Predicate<T> and(Predicate<? super T> other) {
Objects.requireNonNull(other);
return (t) -> test(t) && other.test(t);
} /**
* Returns a predicate that represents the logical negation of this
* predicate.
*/
default Predicate<T> negate() {
return (t) -> !test(t);
} /**
* Returns a composed predicate that represents a short-circuiting logical
* OR of this predicate and another. When evaluating the composed
* predicate, if this predicate is {@code true}, then the {@code other}
* predicate is not evaluated.
*/
default Predicate<T> or(Predicate<? super T> other) {
Objects.requireNonNull(other);
return (t) -> test(t) || other.test(t);
} /**
* Returns a predicate that tests if two arguments are equal according
* to {@link Objects#equals(Object, Object)}.
*/
static <T> Predicate<T> isEqual(Object targetRef) {
return (null == targetRef)
? Objects::isNull
: object -> targetRef.equals(object);
}
}

Predicate是个断言式接口其参数是<T,boolean>,也就是给一个参数T,返回boolean类型的结果。跟Function一样,Predicate的具体实现也是根据传入的lambda表达式来决定的。

boolean test(T t);

接下来我们看看Predicate默认实现的三个重要方法and,or和negate

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

这三个方法对应了java的三个连接符号&&、||和!,基本的使用十分简单,我们给一个例子看看:

int[] numbers= {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
List<Integer> list=new ArrayList<>();
for(int i:numbers) {
list.add(i);
}
Predicate<Integer> p1=i->i>5;
Predicate<Integer> p2=i->i<20;
Predicate<Integer> p3=i->i%2==0;
List test=list.stream().filter(p1.and(p2).and(p3)).collect(Collectors.toList());
System.out.println(test.toString());
/** print:[6, 8, 10, 12, 14]*/

我们定义了三个断言p1,p2,p3。现在有一个从1~15的list,我们需要过滤这个list。上述的filter是过滤出所有大于5小于20,并且是偶数的列表。

假如突然我们的需求变了,我们现在需要过滤出奇数。那么我不可能直接去改Predicate,因为实际项目中这个条件可能在别的地方也要使用。那么此时我只需要更改filter中Predicate的条件。

List test=list.stream().filter(p1.and(p2).and(p3.negate())).collect(Collectors.toList());
/** print:[7, 9, 11, 13, 15]*/

我们直接对p3这个条件取反就可以实现了。是不是很简单?

isEqual这个方法的返回类型也是Predicate,所以我们也可以把它作为函数式接口进行使用。我们可以当做==操作符来使用。

		List test=list.stream()
.filter(p1.and(p2).and(p3.negate()).and(Predicate.isEqual(7)))
.collect(Collectors.toList());
/** print:[7] */

最新文章

  1. PHP中CURL方法curl_setopt()函数的一些参数
  2. 代码中AggregateException的处理
  3. codevs1842 递归第一次
  4. webApi实践:开始WebApi 2
  5. HashMap实现原理分析
  6. POJ - 2339 Rock, Scissors, Paper
  7. notepad++ 正则表达式
  8. HDU 1003 - Max Sum(难度:*)
  9. Android使用Google推荐的联网框架Volley,让连接网络更加简单
  10. SpringMVC1
  11. MyBatis学习(一)简介及入门案例
  12. swift UIview上添加视频播放
  13. 看到一个想收藏的的AJAX小列子
  14. Person Re-ID行人重试别数据集
  15. 详解 JVM Garbage First(G1) 垃圾收集器(转载)
  16. day41 mysql详细操作
  17. SGD vs Momentum vs NAG vs Adagrad vs Adadelta vs RMSprop vs Adam
  18. PHP加密与编码技术
  19. 转:vue+element实现树形组件
  20. jmeter3.2 版本完美实现Load Test报表

热门文章

  1. POJ_3368_Frequent values
  2. iOS电话等中断事件的开始和结束通知
  3. 分析 mongodb admin local 修改ip 热修改
  4. 关于LeNet-5卷积神经网络 S2层与C3层连接的参数计算的思考???
  5. MongoDB的用户管理命令
  6. 新建的web工程找不到javax.servlet.http.httpservlet
  7. 虫师的性能测试思想html网页学习
  8. gitlab启用https的配置
  9. python-面向对象-09_类属性和类方法
  10. xpath教程 1 - 什么是XPath