当前位置: 移动技术网 > IT编程>开发语言>Java > Java Predicate及Consumer接口函数代码实现解析

Java Predicate及Consumer接口函数代码实现解析

2020年06月14日  | 移动技术网IT编程  | 我要评论

predicate函数编程

predicate功能判断输入的对象是否符合某个条件。官方文档解释到:determines if the input object matches some criteria.

了解predicate接口作用后,在学习predicate函数编程前,先看一下java 8关于predicate的源码:

@functionalinterface
public interface predicate<t> {

  /**
   * 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);

  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);
  }


  static <t> predicate<t> isequal(object targetref) {
    return (null == targetref)
        ? objects::isnull
        : object -> targetref.equals(object);
  }
}

从上面代码可以发现,java 8新增了接口的默认(default)方法和(static)静态方法。在java 8以前,接口里的方法要求全部是抽象方法。但是静态(static)方法只能通过接口名调用,不可以通过实现类的类名或者实现类的对象调用;默认(default)方法只能通过接口实现类的对象来调用。

接下来主要来使用接口方法test,可以使用匿名内部类提供test()方法的实现,也可以使用lambda表达式实现test()。
体验一下predicate的函数式编程,使用lambda实现。其测试代码如下:

@test
public void testpredicate(){
  java.util.function.predicate<integer> boolvalue = x -> x > 5;
  system.out.println(boolvalue.test(1));//false
  system.out.println(boolvalue.test(6));//true
}

第1行代码:定义一个predicate实现,入参为integer,返回传入参数与5做比较。
第2,3行代码调用第一行,传入相关参数。

consumer函数编程

consumer接口的文档声明如下:

an operation which accepts a single input argument and returns no result. unlike most other functional interfaces, consumer is expected to operate via side-effects.

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

同样,在了解consumer函数编程前,看一下consumer源代码,其源代码如下:

@functionalinterface
public interface consumer<t> {

  /**
   * performs this operation on the given argument.
   *
   * @param t the input argument
   */
  void accept(t t);

  /**
   * returns a composed {@code consumer} that performs, in sequence, this
   * operation followed by the {@code after} operation. if performing either
   * operation throws an exception, it is relayed to the caller of the
   * composed operation. if performing this operation throws an exception,
   * the {@code after} operation will not be performed.
   *
   * @param after the operation to perform after this operation
   * @return a composed {@code consumer} that performs in sequence this
   * operation followed by the {@code after} operation
   * @throws nullpointerexception if {@code after} is null
   */
  default consumer<t> andthen(consumer<? super t> after) {
    objects.requirenonnull(after);
    return (t t) -> { accept(t); after.accept(t); };
  }
}

从上面代码可以看出,consumer使用了java 8接口新特性——接口默认(default)方法。接下来使用接口方法accept,体验一下consumer函数编程。其测试代码如下:

@test
public void testconsumer(){
  user user = new user("zm");
  //接受一个参数
  consumer<user> userconsumer = user1 -> user1.setname("zmchange");
  userconsumer.accept(user);
  system.out.println(user.getname());//zmchange
}

在java 8之前的实现如下:

@test
public void test(){
  user user = new user("zm");
  this.change(user);
  system.out.println(user.getname());//输出zmchange
}

private void change(user user){
  user.setname("zmchange");
}

predicate和consumer综合应用

为了详细说明predicate和consumer接口,通过一个学生例子:student类包含姓名、分数以及待付费用,每个学生可根据分数获得不同程度的费用折扣。

student类源代码:

public class student {

  string firstname;

  string lastname;

  double grade;

  double feediscount = 0.0;
  double basefee = 2000.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对象的predicate接口以及consumer接口的实现类。本例子使用predicate接口实现类的test()方法判断输入的student对象是否拥有费用打折的资格,然后使用consumer接口的实现类更新输入的student对象的折扣。

public class predicateconsumerdemo {

  public static student updatestudentfee(student student, predicate<student> predicate, consumer<student> consumer){
    if (predicate.test(student)){
      consumer.accept(student);
    }
    return student;
  }

}

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

public class test {
  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(); //the fee after discount: 1400.0

    student student2 = new student("rajat","verma", 8.0);
    student2 = updatestudentfee(student2,
        //lambda expression for predicate interface
        student -> student.grade >= 8,
        //lambda expression for consumer inerface
        student -> student.feediscount = 20.0);
    student2.printfee();//the fee after discount: 1600.0

  }
}

通过简单对predicate接口和consumer接口进行应用,对函数式编程有了一个直观认识。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网