1. 程式人生 > >Predicate和Consumer介面– Java 8中java.util.function包下的介面

Predicate和Consumer介面– Java 8中java.util.function包下的介面

原文連結 作者:   Mohamed Sanaulla  譯者: 李璟([email protected])

早先我寫了一篇《函式式介面》,探討了部分Java 8中函式式介面的用法。我也提及了Predicate介面屬於java.util.function包,在這篇文章中,我將展示如何應用Predicate介面和Consumer介面。

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

Determines if the input object matches some criteria.

即判斷輸入的物件是否符合某個條件。

在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()方法是抽象方法以外,其他方法都是預設方法(譯者注:在Java 8中,介面可以包含帶有實現程式碼的方法,這些方法稱為default方法)。可以使用匿名內部類提供test()方法的實現,也可以使用lambda表示式實現test()。

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介面中有2個方法,有且只有一個宣告為accept(T t)的方法,接收一個輸入引數並且沒有返回值。為了詳細說明Predicate和Consumer介面,我們來考慮一下學生的例子:Student類包含姓名,分數以及待付費用,每個學生可根據分數獲得不同程度的費用折扣。

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物件的Predicate介面以及Consumer介面的實現類。如果你還不熟悉Function介面,那麼你需要花幾分鐘閱讀一下這篇文章。這個例子使用Predicate介面實現類的test()方法判斷輸入的Student物件是否擁有費用打折的資格,然後使用Consumer介面的實現類更新輸入的Student物件的折扣。

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;

    }

}

Predicate和Consumer介面的test()和accept()方法都接受一個泛型引數。不同的是test()方法進行某些邏輯判斷並返回一個boolean值,而accept()接受並改變某個物件的內部值。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();

}