亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術文章
文章詳情頁

Java Predicate及Consumer接口函數代碼實現解析

瀏覽:10日期:2022-08-31 14:19:50

Predicate函數編程

Predicate功能判斷輸入的對象是否符合某個條件。官方文檔解釋到:Determines if the input object matches some criteria.

了解Predicate接口作用后,在學習Predicate函數編程前,先看一下Java 8關于Predicate的源碼:

@FunctionalInterfacepublic 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實現。其測試代碼如下:

@Testpublic 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源代碼,其源代碼如下:

@FunctionalInterfacepublic 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函數編程。其測試代碼如下:

@Testpublic 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之前的實現如下:

@Testpublic 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 interfacestudent -> student.grade > 8.5,//Lambda expression for Consumer inerfacestudent -> 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 interfacestudent -> student.grade >= 8,//Lambda expression for Consumer inerfacestudent -> student.feeDiscount = 20.0); student2.printFee();//The fee after discount: 1600.0 }}

通過簡單對Predicate接口和Consumer接口進行應用,對函數式編程有了一個直觀認識。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: Java
相關文章:
主站蜘蛛池模板: 国产欧美一区二区精品性色 | 中文字幕国产一区 | 日韩视频 中文字幕 | 玖玖99 | 在线观看免费黄视频 | 国语自产免费精品视频一区二区 | 午夜影院一级片 | 91视频久久| 国产91av在线 | 青青草91视频| 亚州毛色毛片免费观看 | 好看的亚洲视频 | 亚洲欧美日韩在线一区 | yiren22开心综合成人网 | 国产成人综合在线观看网站 | 最新99国产成人精品视频免费 | 黄色一级短视频 | 4虎成人 | 日本黄色大片免费观看 | 国内自拍网址 | 日韩专区在线播放 | 国产精品亚洲国产 | 给我一个可以看片的www日本 | 青青青网 | 黄色成年视频 | 国产福利在线观看永久视频 | 九色国产在视频线精品视频 | 欧美一级高清在线观看 | 国产精品国产三级国产在线观看 | 国产成人永久免费视 | 国产欧美日韩一区二区三区 | 一区二区三区在线免费观看视频 | 国产美女自拍 | 国产男人午夜视频在线观看 | 国产高清在线a视频大全凹凸 | 毛片毛片免费看 | 免费a级片在线观看 | 自拍第一页 | 国产产一区二区三区久久毛片国语 | 人人综合 | 成人看片黄a在线看 |