Java8 predicate and_Java8-6-Predicate接口详解

Predicate函数式接口的主要作用就是提供一个test方法,接受一个参数返回一个布尔类型,Predicate在stream api中进行一些判断的时候非常常用。

@FunctionalInterfacepublic interface Predicate{/**

* Evaluates this predicate on the given argument.

*

* @param t the input argument

* @return {@code true} if the input argument matches the predicate,

* otherwise {@code false}*/boolean test(T t);

}

使用泛型T指定传入的参数类型,我们通过一个根据不同条件取出不同数据的例子来看下Predicate具体应用

public classPredicateTest {public static voidmain(String[] args) {

List list = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);

PredicateTest predicateTest= newPredicateTest();//输出大于5的数字

List result = predicateTest.conditionFilter(list, integer -> integer > 5);

result.forEach(System.out::println);

System.out.println("-------");//输出大于等于5的数字

result = predicateTest.conditionFilter(list, integer -> integer >= 5);

result.forEach(System.out::println);

System.out.println("-------");//输出小于8的数字

result = predicateTest.conditionFilter(list, integer -> integer < 8);

result.forEach(System.out::println);

System.out.println("-------");//输出所有数字

result = predicateTest.conditionFilter(list, integer -> true);

result.forEach(System.out::println);

System.out.println("-------");

}//高度抽象的方法定义,复用性高

public List conditionFilter(List list, Predicatepredicate){returnlist.stream().filter(predicate).collect(Collectors.toList());

}

}

我们只定义了一个conditionFilter方法,stream()会将当前list作为源创建一个Stream对象,collect(Collectors.toList())是将最终的结果封装在ArrayList中(这部分会在后续stream学习中详细介绍,这里只关注filter即可),filter方法接收一个Predicate类型参数用于对目标集合进行过滤。里面并没有任何具体的逻辑,提供了一种更高层次的抽象化,我们可以把要处理的数据和具体的逻辑通过参数传递给conditionFilter即可。理解了这种设计思想后,再看上面的例子就很容易理解,本身逻辑并不复杂,分别取出小于5、大于等于5、小于8的元素,最后一个总是返回true的条件意味着打印出集合中所有元素。

除此之外,Predicate还新增了接口的默认(default)方法和(static)静态方法。在Java 8以前,接口里的方法要求全部是抽象方法。但是静态(static)方法只能通过接口名调用,不可以通过实现类的类名或者实现类的对象调用;默认(default)方法只能通过接口实现类的对象来调用。

default Predicate and(Predicate super T>other) {

Objects.requireNonNull(other);return (t) -> test(t) &&other.test(t);

}default Predicate or(Predicate super T>other) {

Objects.requireNonNull(other);return (t) -> test(t) ||other.test(t);

}default Predicatenegate() {return (t) -> !test(t);

}static PredicateisEqual(Object targetRef) {return (null ==targetRef)?Objects::isNull

:object -> targetRef.equals(object);

}

and方法接收一个Predicate类型,也就是将传入的条件和当前条件以并且的关系过滤数据。or方法同样接收一个Predicate类型,将传入的条件和当前的条件以或者的关系过滤数据。negate就是将当前条件取反。看下具体使用方式

public List conditionFilterNegate(List list, Predicatepredicate){returnlist.stream().filter(predicate.negate()).collect(Collectors.toList());

}public List conditionFilterAnd(List list, Predicate predicate,Predicatepredicate2){returnlist.stream().filter(predicate.and(predicate2)).collect(Collectors.toList());

}public List conditionFilterOr(List list, Predicate predicate,Predicatepredicate2){returnlist.stream().filter(predicate.or(predicate2)).collect(Collectors.toList());

}//大于5并且是偶数

result = predicateTest.conditionFilterAnd(list, integer -> integer > 5, integer1 -> integer1 % 2 == 0);

result.forEach(System.out::println);//6 8 10

System.out.println("-------");//大于5或者是偶数

result = predicateTest.conditionFilterOr(list, integer -> integer > 5, integer1 -> integer1 % 2 == 0);

result.forEach(System.out::println);//2 4 6 8 9 10

System.out.println("-------");//条件取反

result = predicateTest.conditionFilterNegate(list,integer2 -> integer2 > 5);

result.forEach(System.out::println);//1 2 3 4 5

System.out.println("-------");

我们分别借助Predicate的三个默认方法定义了conditionFilterAnd、conditionFilterOr和conditionFilterNegate方法。然后再下方调用这三个方法,根据传入的判断条件观察输出结果。

最后再来看一下Predicate接口中的唯一一个静态方法,Java8中接口中除了增加了默认方法也可以定义静态方法。

/**

* Returns a predicate that tests if two arguments are equal according

* to {@link Objects#equals(Object, Object)}.

*

* @param the type of arguments to the predicate

* @param targetRef the object reference with which to compare for equality,

* which may be {@code null}

* @return a predicate that tests if two arguments are equal according

* to {@link Objects#equals(Object, Object)}*/

static PredicateisEqual(Object targetRef) {return (null ==targetRef)?Objects::isNull

:object -> targetRef.equals(object);

}

isEqual方法返回类型也是Predicate,也就是说通过isEqual方法得到的也是一个用来进行条件判断的函数式接口实例。而返回的这个函数式接口实例是通过传入的targetRef的equals方法进行判断的。我们看一下具体用法

System.out.println(Predicate.isEqual("test").test("test"));//true

这里会用第一个"test"的equals方法判断与第二个"test"是否相等,结果true。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值