Lambda表達(dá)式,也可以稱為閉包,它時(shí)推動(dòng)Java8發(fā)布的最重要新特性。
Lambda運(yùn)行把函數(shù)作為一個(gè)方法的參數(shù),使代碼變得更加簡(jiǎn)介緊湊。
1.Lambda表達(dá)式格式:? ->:Lambda操作符
? 左邊是Lambda形參列表,也就是接口中抽象方法的形參列表。
? 右邊是Lambda體,也就是重寫(xiě)的抽象方法的方法體。
2.Lambda表達(dá)式的使用形式有6種 2.1語(yǔ)法格式一:無(wú)參,無(wú)返回值@Test
public void test0(){Runnable r1 = new Runnable() {@Override
public void run() {System.out.println("I love you.");
}
};
r1.run();
System.out.println("*****************");
Runnable r2 = () ->System.out.println("I love yy.");//Lambda表達(dá)式
r2.run();
}
2.2語(yǔ)法格式二:Lambda需要一個(gè)參數(shù),但沒(méi)有返回值@Test
public void test2(){Consumercon = new Consumer() {@Override
public void accept(String s) {System.out.println(s);
}
};
con.accept("I love you not because of who you are, but because of who I am when I am with you.");
System.out.println("****************");
Consumercon1 = (String s) ->{System.out.println(s);
};
con1.accept("Just because someone doesn't love you the way you want them to, doesn't mean they don't love you with all they have.");
}
2.3語(yǔ)法格式三:數(shù)據(jù)類型可以省略,因?yàn)榭捎删幾g器推斷得出(類型推斷,泛型的時(shí)候也是如此)@Test
public void test3(){Consumercon = new Consumer() {@Override
public void accept(String s) {System.out.println(s);
}
};
con.accept("No man is worth your tears, and the one who is won't make you cry.");
System.out.println("****************");
Consumercon1 = (s) ->{System.out.println(s);
};
con1.accept("Never frown, even when you are sad.");
}
2.4語(yǔ)法格式四:Lambda若自需要一個(gè)參數(shù)時(shí),參數(shù)的小括號(hào)可以省略@Test
public void test4(){Consumercon = new Consumer() {@Override
public void accept(String s) {System.out.println(s);
}
};
con.accept("Don't cry because it is over.");
System.out.println("****************");
Consumercon1 = s ->{System.out.println(s);
};
con1.accept("Smile because it happened, life is a pure flame, and i love you always.");
}
2.5語(yǔ)法格式五:Lambda需要兩個(gè)或以上的參數(shù),多條執(zhí)行語(yǔ)句,并且可以有返回值@Test
public void test1(){Comparatorcom1 = new Comparator() {@Override
public int compare(Integer o1, Integer o2) {System.out.println(o1);
System.out.println(o2);
return o1.compareTo(o2);
}
};
int compare1 = com1.compare(12,21);
System.out.println(compare1);//-1
System.out.println("****************");
//lambda表達(dá)式
Comparatorcom2 = (o1,o2)->{System.out.println(o1);
System.out.println(o2);
return o1.compareTo(o2);
};
int compare2 = com2.compare(32,21);
System.out.println(compare2);//1
//方法引用
Comparatorcom3 = Integer ::compare;
int compare3 = com3.compare(32,21);
System.out.println(compare3);//1
}
2.6語(yǔ)法格式六:Lambda體只有一條執(zhí)行語(yǔ)句時(shí),return與大括號(hào)若有,都可以省略@Test
public void test5(){Comparatorcom1 = new Comparator() {@Override
public int compare(Integer o1, Integer o2) {return o1.compareTo(o2);
}
};
int compare1 = com1.compare(12,21);
System.out.println(compare1);//-1
System.out.println("****************");
//lambda表達(dá)式
Comparatorcom2 = (o1,o2)->o1.compareTo(o2);
int compare2 = com2.compare(32,21);
System.out.println(compare2);//1
}
2.7Lambda表達(dá)式總結(jié):->左邊:lambda 形參列表的參數(shù)類型可以省略(類型推斷):如果lambda形參列表只有一個(gè)參數(shù),可以省略小
括號(hào)。
->右邊:lambda 體應(yīng)該使用一對(duì){}包裹,如果lambda只有一條執(zhí)行語(yǔ)句,包含return語(yǔ)句,可以省略大括號(hào)。
3.Lambda表達(dá)式的本質(zhì):作為接口的實(shí)例,該接口應(yīng)當(dāng)為函數(shù)式接口(FunctionalInterface),只有一種抽象方法。你是否還在尋找穩(wěn)定的海外服務(wù)器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機(jī)房具備T級(jí)流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級(jí)服務(wù)器適合批量采購(gòu),新人活動(dòng)首月15元起,快前往官網(wǎng)查看詳情吧
本文標(biāo)題:JavaLambda表達(dá)式-創(chuàng)新互聯(lián)
文章URL:http://aaarwkj.com/article26/ihhcg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供虛擬主機(jī)、網(wǎng)站內(nèi)鏈、品牌網(wǎng)站建設(shè)、網(wǎng)站收錄、營(yíng)銷型網(wǎng)站建設(shè)、手機(jī)網(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í)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容