Java Predicate及Consumer接口函數(shù)代碼實(shí)現(xiàn)解析
Predicate函數(shù)編程
Predicate功能判斷輸入的對(duì)象是否符合某個(gè)條件。官方文檔解釋到:Determines if the input object matches some criteria.
了解Predicate接口作用后,在學(xué)習(xí)Predicate函數(shù)編程前,先看一下Java 8關(guān)于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); } }
從上面代碼可以發(fā)現(xiàn),Java 8新增了接口的默認(rèn)(default)方法和(static)靜態(tài)方法。在Java 8以前,接口里的方法要求全部是抽象方法。但是靜態(tài)(static)方法只能通過(guò)接口名調(diào)用,不可以通過(guò)實(shí)現(xiàn)類的類名或者實(shí)現(xiàn)類的對(duì)象調(diào)用;默認(rèn)(default)方法只能通過(guò)接口實(shí)現(xiàn)類的對(duì)象來(lái)調(diào)用。
接下來(lái)主要來(lái)使用接口方法test,可以使用匿名內(nèi)部類提供test()方法的實(shí)現(xiàn),也可以使用lambda表達(dá)式實(shí)現(xiàn)test()。
體驗(yàn)一下Predicate的函數(shù)式編程,使用lambda實(shí)現(xiàn)。其測(cè)試代碼如下:
@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行代碼:定義一個(gè)Predicate實(shí)現(xiàn),入?yún)镮nteger,返回傳入?yún)?shù)與5做比較。
第2,3行代碼調(diào)用第一行,傳入相關(guān)參數(shù)。
Consumer函數(shù)編程
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.
即接口表示一個(gè)接受單個(gè)輸入?yún)?shù)并且沒(méi)有返回值的操作。不像其它函數(shù)式接口,Consumer接口期望執(zhí)行帶有副作用的操作(Consumer的操作可能會(huì)更改輸入?yún)?shù)的內(nèi)部狀態(tài))。
同樣,在了解Consumer函數(shù)編程前,看一下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接口新特性——接口默認(rèn)(default)方法。接下來(lái)使用接口方法accept,體驗(yàn)一下Consumer函數(shù)編程。其測(cè)試代碼如下:
@Test public void testConsumer(){ User user = new User("zm"); //接受一個(gè)參數(shù) Consumer<User> userConsumer = User1 -> User1.setName("zmChange"); userConsumer.accept(user); System.out.println(user.getName());//zmChange }
在Java 8之前的實(shí)現(xiàn)如下:
@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綜合應(yīng)用
為了詳細(xì)說(shuō)明Predicate和Consumer接口,通過(guò)一個(gè)學(xué)生例子:Student類包含姓名、分?jǐn)?shù)以及待付費(fèi)用,每個(gè)學(xué)生可根據(jù)分?jǐn)?shù)獲得不同程度的費(fèi)用折扣。
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); } }
然后分別聲明一個(gè)接受Student對(duì)象的Predicate接口以及Consumer接口的實(shí)現(xiàn)類。本例子使用Predicate接口實(shí)現(xiàn)類的test()方法判斷輸入的Student對(duì)象是否擁有費(fèi)用打折的資格,然后使用Consumer接口的實(shí)現(xiàn)類更新輸入的Student對(duì)象的折扣。
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()方法都接受一個(gè)泛型參數(shù)。不同的是test()方法進(jìn)行某些邏輯判斷并返回一個(gè)boolean值,而accept()接受并改變某個(gè)對(duì)象的內(nèi)部值。updateStudentFee方法的調(diào)用如下所示:
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 } }
通過(guò)簡(jiǎn)單對(duì)Predicate接口和Consumer接口進(jìn)行應(yīng)用,對(duì)函數(shù)式編程有了一個(gè)直觀認(rèn)識(shí)。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot如何實(shí)現(xiàn)word文檔轉(zhuǎn)pdf
這篇文章主要介紹了SpringBoot如何實(shí)現(xiàn)word文檔轉(zhuǎn)pdf,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07spring接口通過(guò)配置支持返回多種格式(xml,json,html,excel)
這篇文章主要給大家介紹了關(guān)于spring接口如何通過(guò)配置支持返回多種格式(xml,json,html,excel)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2017-12-12BCryptPasswordEncoder加密與MD5加密的區(qū)別及說(shuō)明
這篇文章主要介紹了BCryptPasswordEncoder加密與MD5加密的區(qū)別及說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-08-08解讀jdk動(dòng)態(tài)代理為什么必須實(shí)現(xiàn)接口
這篇文章主要介紹了解讀jdk動(dòng)態(tài)代理為什么必須實(shí)現(xiàn)接口問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-02-02Javaweb動(dòng)態(tài)開發(fā)最重要的Servlet詳解
動(dòng)態(tài)web的核心是Servlet,由tomcat解析并執(zhí)行,本質(zhì)是Java中的一個(gè)類(面向?qū)ο螅┻@個(gè)類的功能十分強(qiáng)大幾乎可以完成全部功能,在Java規(guī)范中只有Servlet實(shí)現(xiàn)類實(shí)例化的對(duì)象才能被瀏覽器訪問(wèn),所以掌握Servlet具有重要意義2022-08-08