java.util接口_Java 8中java.util.function包中的谓词和使用者接口

java.util接口

在上一篇文章中,我写了关于Function接口的内容 ,它是java.util.package的一部分。 我还提到了Predicate接口,它是同一包的一部分,在这篇文章中,我将向您展示如何使用PredicateConsumer接口。 让我们看一下Javadoc for Predicate接口:

确定输入对象是否符合某些条件。

并且在该接口中声明/定义了5种方法(您一定想知道这是一个功能性接口 ,如果是,则必须在继续之前阅读此方法),这些方法是:

//Returns a predicate which evaluates to true only if this predicate
//and the provided predicate both evaluate to true.
and(Predicate<? super T> p)
 
//Returns a predicate which negates the result of this predicate.
negate()
 
//Returns a predicate which evaluates to true if either
//this predicate or the provided predicate evaluates to true
or(Predicate<? super T> p)
 
//Returns true if the input object matches some criteria
test(T t)
 
//Returns a predicate that evaluates to true if both or neither
//of the component predicates evaluate to true
xor(Predicate<? super T> p)

除test(T t)以外的所有方法均为默认方法,而test(T t)为抽象方法。 提供此抽象方法实现的一种方法是使用匿名内部类,另一种方法是使用lambda表达式

用于消费者接口的Javadoc指出:

接受单个输入参数且不返回结果的操作。 与大多数其他功能接口不同,消费者应该通过副作用进行操作。

此接口中有2种方法,其中只有一种是抽象的,而该抽象方法是:accept(T t),它接受输入并且不返回任何结果。 要解释有关谓词和消费者界面的更多信息,我们考虑一个带有名称,等级和要支付费用的学生班。 每个学生都有一定的折扣,折扣取决于学生的成绩。

class Student{
  String firstName;
  String lastName;
  Double grade;
  Double feeDiscount = 0.0;
  Double baseFee = 20000.0;
 
  public Student(String firstName, String lastName,
                 Double grade) {
    this.firstName = firstName;
    this.lastName = lastName;
    this.grade = grade;
  }
   
  public void printFee(){
    Double newFee = baseFee - ((baseFee*feeDiscount)/100);
    System.out.println("The fee after discount: "+newFee);
  }
}

并让我们创建一个接受Student对象,谓词实现和Consumer实现的方法。 如果您不熟悉Function接口,那么您应该花几分钟阅读文档。 此方法使用谓词来确定是否必须更新学生对费用的折扣,然后使用Consumer实现来更新折扣。

public class PreidcateConsumerDemo {
   
  public static Student updateStudentFee(Student student,
          Predicate<Student> predicate,
          Consumer<Student> consumer){
     
    //Use the predicate to decide when to update the discount.
    if ( predicate.test(student)){
      //Use the consumer to update the discount value.
      consumer.accept(student);
    }
    return student;
  }
 
}

谓词和使用者中的测试方法和接受方法都分别接受声明的泛型类型的参数。 两者之间的区别在于谓词使用参数来做出决定并返回布尔值,而消费者使用参数来改变其某些值。 让我们看一下如何调用updateStudentFee方法:

public static void main(String[] args) {
 
  Student student1 = new Student("Ashok","Kumar", 9.5);
  student1 = updateStudentFee(student1,
                      //Lambda expression for Predicate interface
                      student -> student.grade > 8.5,
                      //Lambda expression for Consumer inerface
                      student -> student.feeDiscount = 30.0);
  student1.printFee();
   
  Student student2 = new Student("Rajat","Verma", 8.0);
  student2 = updateStudentFee(student2,
                      student -> student.grade >= 8,
                      student -> student.feeDiscount = 20.0);
  student2.printFee();
   
}

在这篇文章中,我通过示例解释了如何利用Predicate和Consumer接口,它们是Java 8中引入的java.util.function包的一部分。


翻译自: https://www.javacodegeeks.com/2013/04/predicate-and-consumer-interface-in-java-util-function-package-in-java-8.html

java.util接口

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值