閱讀869 返回首頁    go 阿裏雲 go 技術社區[雲棲]


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

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

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

Determines if the input object matches some criteria.

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

在Predicate接口中,有以下5個方法(你肯定好奇為何此接口屬於函數式接口。如果你這麼想,在使用接口前應該好好研讀方法的注釋):

01 //Returns a predicate which evaluates to true only if this predicate
02 //and the provided predicate both evaluate to true.
03 and(Predicate<? super T> p) 
04  
05 //Returns a predicate which negates the result of this predicate.
06 negate() 
07  
08 //Returns a predicate which evaluates to true if either
09 //this predicate or the provided predicate evaluates to true
10 or(Predicate<? super T> p) 
11  
12 //Returns true if the input object matches some criteria
13 test(T t) 
14  
15 //Returns a predicate that evaluates to true if both or neither
16 //of the component predicates evaluate to true
17 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類包含姓名,分數以及待付費用,每個學生可根據分數獲得不同程度的費用折扣。

01 class Student{
02  
03     String firstName;
04  
05     String lastName;
06  
07     Double grade;
08  
09     Double feeDiscount = 0.0;
10  
11     Double baseFee = 20000.0;
12  
13     public Student(String firstName, String lastName, Double grade) {
14  
15         this.firstName = firstName;
16  
17         this.lastName = lastName;
18  
19         this.grade = grade;
20     }
21  
22     public void printFee(){
23  
24         Double newFee = baseFee - ((baseFee * feeDiscount) / 100);
25  
26         System.out.println("The fee after discount: " + newFee);
27  
28     }
29  
30 }

我們分別聲明一個接受Student對象的Predicate接口以及Consumer接口的實現類。如果你還不熟悉Function接口,那麼你需要花幾分鍾閱讀一下這篇文章。這個例子使用Predicate接口實現類的test()方法判斷輸入的Student對象是否擁有費用打折的資格,然後使用Consumer接口的實現類更新輸入的Student對象的折扣。

01 public class PreidcateConsumerDemo {
02  
03    public static Student updateStudentFee(Student student, Predicate<Student> predicate, Consumer<Student> consumer){
04  
05         //Use the predicate to decide when to update the discount.
06  
07         if ( predicate.test(student)){
08  
09             //Use the consumer to update the discount value.
10  
11             consumer.accept(student);
12         }
13  
14         return student;
15  
16     }
17  
18 }

Predicate和Consumer接口的test()和accept()方法都接受一個泛型參數。不同的是test()方法進行某些邏輯判斷並返回一個boolean值,而accept()接受並改變某個對象的內部值。updateStudentFee方法的調用如下所示:

01 public static void main(String[] args) {
02  
03     Student student1 = new Student("Ashok","Kumar"9.5);
04  
05     student1 = updateStudentFee(student1,
06                                 //Lambda expression for Predicate interface
07                                 student -> student.grade > 8.5,
08                                 //Lambda expression for Consumer inerface
09                                 student -> student.feeDiscount = 30.0);
10  
11     student1.printFee();
12  
13     Student student2 = new Student("Rajat","Verma"8.0);
14  
15     student2 = updateStudentFee(student2,
16                                 student -> student.grade >= 8,
17                                 student -> student.feeDiscount = 20.0);
18  
19     student2.printFee();
20  
21 }

最後更新:2017-05-23 10:02:21

  上一篇:go  什麼是上下文切換
  下一篇:go  阿裏內貿團隊敏捷實踐(一)如何打造合作型團隊