Java 8 - Predicate和Consumer接口函数式编程

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接口进行应用,对函数式编程有了一个直观认识。但是还是心里还是有几个问题
– 到底什么是函数式编程?
– 为什么现在好多地方都会提到函数式编程?
– 函数式编程优势在哪里呢?

针对上面三个问题,接下来需要进一步研究。

想简单了解函数式编程,可以参考阮一峰的文章关于函数式编程入门教程

参考资料

JDK8新特性:接口的静态方法和默认方法

java函数式编程之Predicate

java函数式编程之Consumer

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

    原文作者:chuji2012
    原文地址: https://blog.csdn.net/chuji2012/article/details/77871011
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞