Java使用lambda表達(dá)式簡化代碼的示例詳解
代碼,自然寫的越簡潔越好啦,寫的人舒服,看的人也舒服,一切為了高效。
要把有限的時(shí)間花到其它有意思的事情上去。
目的
學(xué)習(xí)簡化代碼的思路,使用jdk8新特性lamada表達(dá)式。
推理一下
某接口,只有一個(gè)方法。
比如這個(gè):
package java.lang; @FunctionalInterface public interface Runnable { void run(); }
或者是這個(gè):
interface MyInterface{ void sayHi(); }
實(shí)現(xiàn)接口,使用方法
一般我們要使用Runable子類開啟一個(gè)線程,要實(shí)現(xiàn)Runnable接口,調(diào)用Threead類的start方法:
public class LeaningLamda implements Runnable{ public static void main(String[] args) { new Thread(new LeaningLamda()).start(); } @Override public void run() { System.out.println(this.getClass()+"我已經(jīng)在跑了!"); } }
或者用sayHi方法干點(diǎn)什么,要先實(shí)現(xiàn)接口,再調(diào)用:
public class LeaningLamda2 implements MyInterface{ public static void main(String[] args) { new LeaningLamda2().sayHi(); } @Override public void sayHi() { System.out.println("ok ok ok ok ,i am say Hi!"); } } interface MyInterface{ void sayHi(); }
內(nèi)部類實(shí)現(xiàn)接口,使用方法
如果這個(gè)方法我就用一次,那我可以在內(nèi)部類中實(shí)現(xiàn)它,提高程序性能:
public class LeaningLamda{ static class MyRun implements Runnable{ @Override public void run() { System.out.println(this.getClass()+"我已經(jīng)在跑了!"); } } public static void main(String[] args) { new Thread(new MyRun()).start(); } }
一樣的用sayHi方法:
public class LeaningLamda2 { static class MyHi implements MyInterface{ @Override public void sayHi() { System.out.println("ok ok ok ok ,i am say Hi!"); } } public static void main(String[] args) { new MyHi().sayHi(); } } interface MyInterface{ void sayHi(); }
局部內(nèi)部類實(shí)現(xiàn)接口,使用方法
既然只使用一次,那我為啥不把它放在使用的方法里面去,性能不就又UpUpUpUp。
像這樣:
public class LeaningLamda{ public static void main(String[] args) { class MyRun implements Runnable{ @Override public void run() { System.out.println(this.getClass()+"我已經(jīng)在跑了!"); } } //調(diào)用在定義后面,謝謝。 new Thread(new MyRun()).start(); } }
或是這樣:
public class LeaningLamda2 { public static void main(String[] args) { class MyHi implements MyInterface{ @Override public void sayHi() { System.out.println("ok ok ok ok ,i am say Hi!"); } } new MyHi().sayHi(); } } interface MyInterface{ void sayHi(); }
匿名內(nèi)部類實(shí)現(xiàn)接口,使用方法
我就用一次,要什么名字?。磕苌佥斎胍粋€(gè)字符都是賺的。需要借助父類或者接口名來實(shí)現(xiàn)。
你看:
public class LeaningLamda{ public static void main(String[] args) { //需要借助父類或者接口來聲明 new Thread(new Runnable(){ @Override public void run() { System.out.println(this.getClass()+"我已經(jīng)在跑了!"); } }).start(); } }
又如:
public class LeaningLamda2 { public static void main(String[] args) { new MyInterface(){ @Override public void sayHi() { System.out.println("ok ok ok ok ,i am say Hi!"); } }.sayHi(); } } interface MyInterface{ void sayHi(); }
使用lamda表達(dá)式的,實(shí)現(xiàn)方法
jdk 8 看不下去了,給我們提供了一個(gè)更加簡化的方案,你看:
lamda表達(dá)式實(shí)現(xiàn)創(chuàng)建單個(gè)簡單線程:
public class LeaningLamda{ public static void main(String[] args) { new Thread(()-> { System.out.println("我已經(jīng)在跑了!"); } ).start(); } }
lamda表達(dá)式sayHi:
public class LeaningLamda2 { public static void main(String[] args) { //此處需要借助一個(gè)父類或接口對象來存放,調(diào)用它 MyInterface ls = ()->{ System.out.println("ok ok ok ok ,i am say Hi!"); }; ls.sayHi(); } } interface MyInterface{ void sayHi(); }
可帶參數(shù)
lamda表達(dá)式可以帶參數(shù),可以不指定類型,它會自適應(yīng)類型:
public class LeaningLamda2 { public static void main(String[] args) { MyInterface ls = (i,str)->{ System.out.println("int:"+i); System.out.println("String:"+str);}; ls.sayHi(520,"i love you!"); } } interface MyInterface{ void sayHi(int i,String str); }
運(yùn)行結(jié)果:
只有一個(gè)參數(shù)
那我括號都可以省了!
public class LeaningLamda2 { public static void main(String[] args) { MyInterface ls = str-> {System.out.println("String:"+str);}; ls.sayHi("i love you!"); } } interface MyInterface{ void sayHi(String str); }
運(yùn)行結(jié)果:
有返回值
如果有返回值呢,正常返回:
public class LeaningLamda2 { public static void main(String[] args) { MyInterface ls = (String str)-> { String str2 = "最后的贏家是:"+str; return str2;}; System.out.println(ls.sayHi("lurenjia")); } } interface MyInterface{ String sayHi(String str); }
只有一條語句
如果方法只有一條語句,那大括號也沒有必要,可以省略:
public class LeaningLamda2 { public static void main(String[] args) { MyInterface ls = (int i,String str)-> System.out.println("int:"+i+"----String:"+str); ls.sayHi(520,"i love you!"); } } interface MyInterface{ void sayHi(int i,String str); }
究極省略,不能再省了
就一條語句,是返回值:
public class LeaningLamda2 { public static void main(String[] args) { MyInterface ls = str-> "最后的贏家是:"+str; System.out.println(ls.sayHi("中國")); } } interface MyInterface{ String sayHi(String str); }
運(yùn)行結(jié)果:
lamda總結(jié)
常用于創(chuàng)建簡單線程。
1、接口只有一個(gè)方法,可有參數(shù),可有返回值。
2、本方法內(nèi)容簡單,使用較少。
3、基本形式為:
接口或父類 名稱 = (參數(shù)類型1 參數(shù)1,參數(shù)類型2 參數(shù)2,...)->{
內(nèi)容
};
名稱.方法(參數(shù)1,參數(shù)2,...);
4、可以省略的是:
1、lamada參數(shù)類型可省,它自適應(yīng)。
2、方法內(nèi)容只有一條內(nèi)容,大括號可省。
3、內(nèi)容只有一句返回語句,return可省,直接寫值。
到此這篇關(guān)于Java使用lambda表達(dá)式簡化代碼的示例詳解的文章就介紹到這了,更多相關(guān)Java lambda表達(dá)式簡化代碼內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
深入理解JavaWeb中過濾器與監(jiān)聽器的應(yīng)用
這篇文章主要介紹了JavaWeb中過濾器與監(jiān)聽器的應(yīng)用,過濾器能夠?qū)ζヅ涞恼埱蟮竭_(dá)目標(biāo)之前或返回響應(yīng)之后增加一些處理代碼,監(jiān)聽器是一個(gè)接口內(nèi)容由我們實(shí)現(xiàn),會在特定時(shí)間被調(diào)用,感興趣想要詳細(xì)了解可以參考下文2023-05-05Java中BufferedReader類獲取輸入輸入字符串實(shí)例
這篇文章主要介紹了Java中BufferedReader類獲取輸入輸入字符串實(shí)例,分享了相關(guān)代碼示例,小編覺得還是挺不錯(cuò)的,具有一定借鑒價(jià)值,需要的朋友可以參考下2018-02-02Java(SpringBoot)項(xiàng)目打包(構(gòu)建)成Docker鏡像的幾種常見方式
在對Spring Boot應(yīng)用程序進(jìn)行Docker化時(shí),為應(yīng)用程序選擇正確的基礎(chǔ)鏡像非常重要,下面這篇文章主要給大家介紹了關(guān)于Java(SpringBoot)項(xiàng)目打包(構(gòu)建)成Docker鏡像的幾種常見方式,需要的朋友可以參考下2023-12-12spring如何實(shí)現(xiàn)兩個(gè)xml配置文件間的互調(diào)
這篇文章主要介紹了spring如何實(shí)現(xiàn)兩個(gè)xml配置文件間的互調(diào),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-11-11SpringBoot + Spring Security 基本使用及個(gè)性化登錄配置詳解
這篇文章主要介紹了SpringBoot + Spring Security 基本使用及個(gè)性化登錄配置詳解,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-05-05Mybatis中一對多(collection)和一對一(association)的組合查詢使用
這篇文章主要介紹了Mybatis中一對多(collection)和一對一(association)的組合查詢使用,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12