Java 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介面進行應用,對函數語言程式設計有了一個直觀認識。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。