這篇文章主要介紹了java8中predicate的使用方法,具有一定借鑒價(jià)值,需要的朋友可以參考下。希望大家閱讀完這篇文章后大有收獲。下面讓小編帶著大家一起了解一下。
創(chuàng)新互聯(lián)公司于2013年開始,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目網(wǎng)站設(shè)計(jì)制作、成都網(wǎng)站制作網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個(gè)夢(mèng)想脫穎而出為使命,1280元玉環(huán)做網(wǎng)站,已為上家服務(wù),為玉環(huán)各地企業(yè)和個(gè)人服務(wù),聯(lián)系電話:028-86922220
傳遞代碼
我們首先看一個(gè)例子,假設(shè)你有一個(gè) Apple 類,它有一個(gè)getColor方法,還有一個(gè)變量inventory保存著一個(gè)Apples的列表。你可能想要選出所有的綠蘋果,并返回一個(gè)列表。通常我們用篩選(filter)一詞來表達(dá)這個(gè)概念。在Java 8之前,你可能會(huì)寫這樣一個(gè)方法 filterGreenApples :
public static List<Apple> filterGreenApples(List<Apple> inventory){ List<Apple> result = new ArrayList<>(); for (Apple apple: inventory){ if ("green".equals(apple.getColor())) { result.add(apple); } } return result; }
但是接下來,有人可能想要選出重的蘋果,比如超過150克,于是你心情沉重地寫了下面這
個(gè)方法,甚至用了復(fù)制粘貼:
public static List<Apple> filterHeavyApples(List<Apple> inventory){ List<Apple> result = new ArrayList<>(); for (Apple apple: inventory){ if (apple.getWeight() > 150) { result.add(apple); } } return result; }
我們都知道軟件工程中復(fù)制粘貼的危險(xiǎn)——給一個(gè)做了更新和修正,卻忘了另一個(gè)。嘿,這
兩個(gè)方法只有一行不同: if 里面高亮的那行條件。如果這兩個(gè)高亮的方法之間的差異僅僅是接受
的重量范圍不同,那么你只要把接受的重量上下限作為參數(shù)傳遞給 filter 就行了,比如指定
(150, 1000) 來選出重的蘋果(超過150克),或者指定 (0, 80) 來選出輕的蘋果(低于80克)。
但是,我們前面提過了,Java 8會(huì)把條件代碼作為參數(shù)傳遞進(jìn)去,這樣可以避免 filter 方法
出現(xiàn)重復(fù)的代碼。現(xiàn)在你可以寫:
public static boolean isGreenApple(Apple apple) { return "green".equals(apple.getColor()); } public static boolean isHeavyApple(Apple apple) { return apple.getWeight() > 150; } static List<Apple> filterApples(List<Apple> inventory, Predicate<Apple> p) { List<Apple> result = new ArrayList<>(); for (Apple apple: inventory){ if (p.test(apple)) { result.add(apple); } } return result; }
要用它的話,你可以寫:
filterApples(inventory, Apple::isGreenApple);
或者
filterApples(inventory, Apple::isHeavyApple);
什么是謂詞?
前面的代碼傳遞了方法 Apple::isGreenApple (它接受參數(shù) Apple 并返回一個(gè)
boolean )給 filterApples ,后者則希望接受一個(gè) Predicate<Apple> 參數(shù)。詞 謂詞(predicate)
在數(shù)學(xué)上常常用來代表一個(gè)類似函數(shù)的東西,它接受一個(gè)參數(shù)值,并返回 true 或 false 。你
在后面會(huì)看到,Java 8也會(huì)允許你寫 Function<Apple,Boolean> ——在學(xué)校學(xué)過函數(shù)卻沒學(xué)
過謂詞的讀者對(duì)此可能更熟悉,但用 Predicate<Apple> 是更標(biāo)準(zhǔn)的方式,效率也會(huì)更高一
點(diǎn)兒,這避免了把 boolean 封裝在 Boolean 里面。
把方法作為值來傳遞顯然很有用,但要是為類似于 isHeavyApple 和 isGreenApple 這種可
能只用一兩次的短方法寫一堆定義有點(diǎn)兒煩人。不過Java 8也解決了這個(gè)問題,它引入了一套新
記法(匿名函數(shù)或Lambda),讓你可以寫
filterApples(inventory, (Apple a) -> "green".equals(a.getColor()) );
或者
filterApples(inventory, (Apple a) -> a.getWeight() > 150 );
甚至
filterApples(inventory, (Apple a) -> a.getWeight() < 80 ||
"brown".equals(a.getColor()) );
完整的代碼為:
public class FilteringApples1 { public static void main(String[] args) { List<FilteringApples1.Apple> inventory = Arrays.asList(new FilteringApples1.Apple(80, "green"), new FilteringApples1.Apple(155, "green"), new FilteringApples1.Apple(120, "red")); List<FilteringApples1.Apple> greenApples2 = filterApples(inventory, (FilteringApples1.Apple a) -> "green".equals(a.getColor())); System.out.println(greenApples2); // [Apple{color='green', weight=155}] List<FilteringApples1.Apple> heavyApples2 = filterApples(inventory, (FilteringApples1.Apple a) -> a.getWeight() > 150); System.out.println(heavyApples2); // [] List<FilteringApples1.Apple> weirdApples = filterApples(inventory, (FilteringApples1.Apple a) -> a.getWeight() < 80 || "brown".equals(a.getColor())); System.out.println(weirdApples); } public static List<FilteringApples1.Apple> filterApples(List<FilteringApples1.Apple> inventory, Predicate<FilteringApples1.Apple> p) { List<FilteringApples1.Apple> result = new ArrayList<>(); for (FilteringApples1.Apple apple : inventory) { if (p.test(apple)) { result.add(apple); } } return result; } public static class Apple { private int weight = 0; private String color = ""; public Apple(int weight, String color) { this.weight = weight; this.color = color; } public Integer getWeight() { return weight; } public void setWeight(Integer weight) { this.weight = weight; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } public String toString() { return "Apple{" + "color='" + color + '\'' + ", weight=" + weight + '}'; } } }
java8中內(nèi)置filter函數(shù)
static <T> Collection<T> filter(Collection<T> c, Predicate<T> p);
這樣你甚至都不需要寫 filterApples 了,因?yàn)楸热缦惹暗恼{(diào)用
filterApples(inventory, (Apple a) -> a.getWeight() > 150 );
就可以直接調(diào)用庫(kù)方法 filter :
filter(inventory, (Apple a) -> a.getWeight() > 150 );
感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享java8中predicate的使用方法內(nèi)容對(duì)大家有幫助,同時(shí)也希望大家多多支持創(chuàng)新互聯(lián),關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,遇到問題就找創(chuàng)新互聯(lián),詳細(xì)的解決方法等著你來學(xué)習(xí)!
當(dāng)前標(biāo)題:java8中predicate的使用方法
網(wǎng)頁(yè)鏈接:http://www.chinadenli.net/article20/gciijo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供ChatGPT、網(wǎng)站收錄、小程序開發(fā)、網(wǎng)站維護(hù)、移動(dòng)網(wǎng)站建設(shè)、品牌網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)