Java8之lambda表達式基本語法
lambda表達式,即帶有參數(shù)的表達式,為更清晰地理解lambda表達式,先看如下例子:
(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方法對集合進行排序,其中第二個參數(shù)是一個類,準確地說是一個匿名內(nèi)部類,sort方法調(diào)用內(nèi)部類中的compare方法對list進行位置交換,因為java中的參數(shù)類型只能是類或者基本數(shù)據(jù)類型,所以雖然傳入的是一個Comparator類,但是實際上需要傳遞的僅僅是compare方法,lambda表達式專門針對只有一個方法的接口(即函數(shù)式接口),Comparator就是一個函數(shù)式接口
@FunctionalInterface
public interface Comparator<T> {
int compare(T o1, T o2);
}
@FunctionalInterface的作用就是標識一個接口為函數(shù)式接口,此時Comparator里只能有一個抽象方法。
使用lambda表達式之后(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);
}
對于有多個參數(shù)的情況,語法:
1. ambda表達式的基本格式為(x1,x2)->{表達式...};
2. 在上式中,lambda表達式帶有兩個參數(shù),因此兩邊的括號不能省略,而參數(shù)類型可以省略
3. 如果表達式只有一行,那么表達式兩邊的花括號可以省略
另外一個常見的例子是新建一個線程,不使用lambda表達式的寫法為
(3)
public void testThread(){
new Thread(new Runnable() {
@Override
public void run() {
System.out.println("hello, i am thread!");
}
}).start();
}
其中Runnable接口也是一個函數(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表達式的寫法為
(4)
public void testThread_(){
new Thread(()-> System.out.println("hello, i am thread!")).start();
}
對于沒有參數(shù)的情況 ,語法:
1.參數(shù)的括號不能省略,如果只有一句的表達式則可省略花括號和語句結(jié)尾的分號
我們構(gòu)造一個只有一個參數(shù)的函數(shù)式接口
@FunctionalInterface
public interface MyFunctionalInterface {
public void single(String msg);
}
/**
* 需要單個參數(shù)
*/
public static void testOnePar(MyFunctionalInterface myFunctionalInterface){
myFunctionalInterface.single("msg");
}
/**
* 一個參數(shù),可以省略參數(shù)的括號
*/
@Test
public void testOneParameter(){
testOnePar(x-> System.out.println(x));
}
對于只有一個參數(shù)的情況 ,語法:
1.參數(shù)的括號可以省略
在這里我們?yōu)榱搜菔局挥幸粋€參數(shù)的情況自己創(chuàng)建了一個函數(shù)式接口,其實java8中已經(jīng)為我們提供了很多常見的函數(shù)式接口
常見的有
Function:提供任意一種類型的參數(shù),返回另外一個任意類型返回值。 R apply(T t);
Consumer:提供任意一種類型的參數(shù),返回空值。 void accept(T t);
Supplier:參數(shù)為空,得到任意一種類型的返回值。T get();
Predicate:提供任意一種類型的參數(shù),返回boolean返回值。boolean test(T t);
因此針對上面的情況,我們可以直接使用Consumer類,
/**
* 需要單個參數(shù)
*/
public static void testOnePar1(Consumer unaryOperator){
unaryOperator.accept("msg");
}
總結(jié)
以上所述是小編給大家介紹的使用Java8之lambda表達式基本語法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
Java BigDecimal和double示例及相關(guān)問題解析
這篇文章主要介紹了Java BigDecimal和double示例及相關(guān)問題解析,簡單介紹了BigDecimal類的相關(guān)內(nèi)容,分享了兩則相關(guān)實例,對問題進行了分析,具有一定參考價值,需要的朋友可以了解下。2017-11-11
Mybatis利用分頁插件PageHelper快速實現(xiàn)分頁查詢
如果你也在用MyBatis,建議嘗試該分頁插件,這一定是最方便使用的分頁插件,這篇文章主要給大家介紹了關(guān)于Mybatis利用分頁插件PageHelper快速實現(xiàn)分頁查詢的相關(guān)資料,PageHelper是一個Mybatis的分頁插件,負責將已經(jīng)寫好的sql語句,進行分頁加工,需要的朋友可以參考下2021-08-08
SpringBoot動態(tài)修改yml配置文件的方法詳解
這篇文章主要為大家詳細介紹了SpringBoot動態(tài)修改yml配置文件的方法,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助2022-03-03

