在路上

 找回密码
 立即注册
在路上 站点首页 学习 查看内容

Predicate和Consumer接口– Java 8中java.util.function包下的接口

2016-12-16 13:17| 发布者: zhangjf| 查看: 475| 评论: 0

摘要: 原文链接 作者: Mohamed Sanaulla 译者: 李璟(jlee381344197@gmail.com) 早先我写了一篇《函数式接口》,探讨了部分Java 8中函数式接口的用法。我也提及了Predicate接口属于java.util.function包,在这篇文章中,我 ...

原文链接 作者: Mohamed Sanaulla 译者: 李璟(jlee381344197@gmail.com)

早先我写了一篇《函数式接口》,探讨了部分Java 8中函数式接口的用法。我也提及了Predicate接口属于java.util.function包,在这篇文章中,我将展示如何应用Predicate接口和Consumer接口。

一起看一下Predicate的官方文档:

即判断输入的对象是否符合某个条件。

在Predicate接口中,有以下5个方法(你肯定好奇为何此接口属于函数式接口。如果你这么想,在使用接口前应该好好研读方法的注释):

  1. //Returns a predicate which evaluates to true only if this predicate
  2. //and the provided predicate both evaluate to true.
  3. and(Predicate<? super T> p)
  4. //Returns a predicate which negates the result of this predicate.
  5. negate()
  6. //Returns a predicate which evaluates to true if either
  7. //this predicate or the provided predicate evaluates to true
  8. or(Predicate<? super T> p)
  9. //Returns true if the input object matches some criteria
  10. test(T t)
  11. //Returns a predicate that evaluates to true if both or neither
  12. //of the component predicates evaluate to true
  13. xor(Predicate<? super T> p)
复制代码

除了test()方法是抽象方法以外,其他方法都是默认方法(译者注:在Java 8中,接口可以包含带有实现代码的方法,这些方法称为default方法)。可以使用匿名内部类提供test()方法的实现,也可以使用lambda表达式实现test()。

Consumer接口的文档声明如下:

即接口表示一个接受单个输入参数并且没有返回值的操作。不像其他函数式接口,Consumer接口期望执行带有副作用的操作(译者注:Consumer的操作可能会更改输入参数的内部状态)。

Consumer接口中有2个方法,有且只有一个声明为accept(T t)的方法,接收一个输入参数并且没有返回值。为了详细说明Predicate和Consumer接口,我们来考虑一下学生的例子:Student类包含姓名,分数以及待付费用,每个学生可根据分数获得不同程度的费用折扣。

  1. class Student{
  2. String firstName;
  3. String lastName;
  4. Double grade;
  5. Double feeDiscount = 0.0;
  6. Double baseFee = 20000.0;
  7. public Student(String firstName, String lastName, Double grade) {
  8. this.firstName = firstName;
  9. this.lastName = lastName;
  10. this.grade = grade;
  11. }
  12. public void printFee(){
  13. Double newFee = baseFee - ((baseFee * feeDiscount) / 100);
  14. System.out.println("The fee after discount: " + newFee);
  15. }
  16. }
复制代码

我们分别声明一个接受Student对象的Predicate接口以及Consumer接口的实现类。如果你还不熟悉Function接口,那么你需要花几分钟阅读一下这篇文章。这个例子使用Predicate接口实现类的test()方法判断输入的Student对象是否拥有费用打折的资格,然后使用Consumer接口的实现类更新输入的Student对象的折扣。

  1. public class PreidcateConsumerDemo {
  2. public static Student updateStudentFee(Student student, Predicate<Student> predicate, Consumer<Student> consumer){
  3. //Use the predicate to decide when to update the discount.
  4. if ( predicate.test(student)){
  5. //Use the consumer to update the discount value.
  6. consumer.accept(student);
  7. }
  8. return student;
  9. }
  10. }
复制代码

Predicate和Consumer接口的test()和accept()方法都接受一个泛型参数。不同的是test()方法进行某些逻辑判断并返回一个boolean值,而accept()接受并改变某个对象的内部值。updateStudentFee方法的调用如下所示:

  1. public static void main(String[] args) {
  2. Student student1 = new Student("Ashok","Kumar", 9.5);
  3. student1 = updateStudentFee(student1,
  4. //Lambda expression for Predicate interface
  5. student -> student.grade > 8.5,
  6. //Lambda expression for Consumer inerface
  7. student -> student.feeDiscount = 30.0);
  8. student1.printFee();
  9. Student student2 = new Student("Rajat","Verma", 8.0);
  10. student2 = updateStudentFee(student2,
  11. student -> student.grade >= 8,
  12. student -> student.feeDiscount = 20.0);
  13. student2.printFee();
  14. }
复制代码

原创文章,转载请注明: 转载自并发编程网 – ifeve.com

本文链接地址: Predicate和Consumer接口– Java 8中java.util.function包下的接口

最新评论

小黑屋|在路上 ( 蜀ICP备15035742号-1 

;

GMT+8, 2025-7-7 20:17

Copyright 2015-2025 djqfx

返回顶部