Java8之lambda表達(dá)式基本語(yǔ)法
lambda表達(dá)式,即帶有參數(shù)的表達(dá)式,為更清晰地理解lambda表達(dá)式,先看如下例子:
(1)
class Student{ private String name; private Double score; public Student(String name, Double score) { this.name = name; this.score = score; } public String getName() { return name; } public Double getScore() { return score; } public void setName(String name) { this.name = name; } public void setScore(Double score) { this.score = score; } @Override public String toString() { return "{" + "\"name\":\"" + name + "\"" + ", \"score\":\"" + score + "\"" + "}"; } } @Test public void test1(){ List<Student> studentList = new ArrayList<Student>(){ { add(new Student("stu1",100.0)); add(new Student("stu2",97.0)); add(new Student("stu3",96.0)); add(new Student("stu4",95.0)); } }; Collections.sort(studentList, new Comparator<Student>() { @Override public int compare(Student o1, Student o2) { return Double.compare(o1.getScore(),o2.getScore()); } }); System.out.println(studentList); }
(1)中代碼調(diào)用Collections.sort方法對(duì)集合進(jìn)行排序,其中第二個(gè)參數(shù)是一個(gè)類,準(zhǔn)確地說(shuō)是一個(gè)匿名內(nèi)部類,sort方法調(diào)用內(nèi)部類中的compare方法對(duì)list進(jìn)行位置交換,因?yàn)閖ava中的參數(shù)類型只能是類或者基本數(shù)據(jù)類型,所以雖然傳入的是一個(gè)Comparator類,但是實(shí)際上需要傳遞的僅僅是compare方法,lambda表達(dá)式專門針對(duì)只有一個(gè)方法的接口(即函數(shù)式接口),Comparator就是一個(gè)函數(shù)式接口
@FunctionalInterface public interface Comparator<T> { int compare(T o1, T o2); }
@FunctionalInterface的作用就是標(biāo)識(shí)一個(gè)接口為函數(shù)式接口,此時(shí)Comparator里只能有一個(gè)抽象方法。
使用lambda表達(dá)式之后(1)中的代碼改造如下
(2)
public void test1_(){ List<Student> studentList = new ArrayList<Student>(){ { add(new Student("stu1",100.0)); add(new Student("stu2",97.0)); add(new Student("stu3",96.0)); add(new Student("stu4",95.0)); } }; Collections.sort(studentList,(s1,s2)-> Double.compare(s1.getScore(),s2.getScore())); System.out.println(studentList); }
對(duì)于有多個(gè)參數(shù)的情況,語(yǔ)法:
1. ambda表達(dá)式的基本格式為(x1,x2)->{表達(dá)式...};
2. 在上式中,lambda表達(dá)式帶有兩個(gè)參數(shù),因此兩邊的括號(hào)不能省略,而參數(shù)類型可以省略
3. 如果表達(dá)式只有一行,那么表達(dá)式兩邊的花括號(hào)可以省略
另外一個(gè)常見(jiàn)的例子是新建一個(gè)線程,不使用lambda表達(dá)式的寫(xiě)法為
(3)
public void testThread(){ new Thread(new Runnable() { @Override public void run() { System.out.println("hello, i am thread!"); } }).start(); }
其中Runnable接口也是一個(gè)函數(shù)式接口,源碼如下
@FunctionalInterface public interface Runnable { /** * When an object implementing interface <code>Runnable</code> is used * to create a thread, starting the thread causes the object's * <code>run</code> method to be called in that separately executing * thread. * <p> * The general contract of the method <code>run</code> is that it may * take any action whatsoever. * * @see java.lang.Thread#run() */ public abstract void run(); }
將其轉(zhuǎn)換為lambda表達(dá)式的寫(xiě)法為
(4)
public void testThread_(){ new Thread(()-> System.out.println("hello, i am thread!")).start(); }
對(duì)于沒(méi)有參數(shù)的情況 ,語(yǔ)法:
1.參數(shù)的括號(hào)不能省略,如果只有一句的表達(dá)式則可省略花括號(hào)和語(yǔ)句結(jié)尾的分號(hào)
我們構(gòu)造一個(gè)只有一個(gè)參數(shù)的函數(shù)式接口
@FunctionalInterface public interface MyFunctionalInterface { public void single(String msg); } /** * 需要單個(gè)參數(shù) */ public static void testOnePar(MyFunctionalInterface myFunctionalInterface){ myFunctionalInterface.single("msg"); } /** * 一個(gè)參數(shù),可以省略參數(shù)的括號(hào) */ @Test public void testOneParameter(){ testOnePar(x-> System.out.println(x)); }
對(duì)于只有一個(gè)參數(shù)的情況 ,語(yǔ)法:
1.參數(shù)的括號(hào)可以省略
在這里我們?yōu)榱搜菔局挥幸粋€(gè)參數(shù)的情況自己創(chuàng)建了一個(gè)函數(shù)式接口,其實(shí)java8中已經(jīng)為我們提供了很多常見(jiàn)的函數(shù)式接口
常見(jiàn)的有
Function:提供任意一種類型的參數(shù),返回另外一個(gè)任意類型返回值。 R apply(T t);
Consumer:提供任意一種類型的參數(shù),返回空值。 void accept(T t);
Supplier:參數(shù)為空,得到任意一種類型的返回值。T get();
Predicate:提供任意一種類型的參數(shù),返回boolean返回值。boolean test(T t);
因此針對(duì)上面的情況,我們可以直接使用Consumer類,
/** * 需要單個(gè)參數(shù) */ public static void testOnePar1(Consumer unaryOperator){ unaryOperator.accept("msg"); }
總結(jié)
以上所述是小編給大家介紹的使用Java8之lambda表達(dá)式基本語(yǔ)法,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
JPA?查詢?cè)鶶QL轉(zhuǎn)換VO對(duì)象方式
這篇文章主要介紹了JPA?查詢?cè)鶶QL轉(zhuǎn)換VO對(duì)象方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11Java BigDecimal和double示例及相關(guān)問(wèn)題解析
這篇文章主要介紹了Java BigDecimal和double示例及相關(guān)問(wèn)題解析,簡(jiǎn)單介紹了BigDecimal類的相關(guān)內(nèi)容,分享了兩則相關(guān)實(shí)例,對(duì)問(wèn)題進(jìn)行了分析,具有一定參考價(jià)值,需要的朋友可以了解下。2017-11-11Mybatis利用分頁(yè)插件PageHelper快速實(shí)現(xiàn)分頁(yè)查詢
如果你也在用MyBatis,建議嘗試該分頁(yè)插件,這一定是最方便使用的分頁(yè)插件,這篇文章主要給大家介紹了關(guān)于Mybatis利用分頁(yè)插件PageHelper快速實(shí)現(xiàn)分頁(yè)查詢的相關(guān)資料,PageHelper是一個(gè)Mybatis的分頁(yè)插件,負(fù)責(zé)將已經(jīng)寫(xiě)好的sql語(yǔ)句,進(jìn)行分頁(yè)加工,需要的朋友可以參考下2021-08-08Spring關(guān)于@Scheduled限制的問(wèn)題
這篇文章主要介紹了Spring關(guān)于@Scheduled限制的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10SpringBoot動(dòng)態(tài)修改yml配置文件的方法詳解
這篇文章主要為大家詳細(xì)介紹了SpringBoot動(dòng)態(tài)修改yml配置文件的方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助2022-03-03java中實(shí)體類和JSON對(duì)象之間相互轉(zhuǎn)化
Java中關(guān)于Json格式轉(zhuǎn)化Object,Map,Collection類型和String類型之間的轉(zhuǎn)化在我們實(shí)際項(xiàng)目中應(yīng)用的很是普遍和廣泛。最近工作的過(guò)程中也是經(jīng)常有,因此,自己封裝了一個(gè)類分享給大家。2015-05-05idea如何設(shè)置Git忽略對(duì)某些文件或文件夾的版本追蹤
這篇文章主要介紹了idea如何設(shè)置Git忽略對(duì)某些文件或文件夾的版本追蹤問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03