lambda表達式,即帶有參數(shù)的表達式,為更清晰地理解lambda表達式,先看如下例子:
(1)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
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ù)是一個類,準(zhǔn)確地說是一個匿名內(nèi)部類,sort方法調(diào)用內(nèi)部類中的compare方法對list進行位置交換,因為java中的參數(shù)類型只能是類或者基本數(shù)據(jù)類型,所以雖然傳入的是一個Comparator類,但是實際上需要傳遞的僅僅是compare方法,lambda表達式專門針對只有一個方法的接口(即函數(shù)式接口),Comparator就是一個函數(shù)式接口
1
2
3
4
|
@FunctionalInterface public interface Comparator<T> { int compare(T o1, T o2); } |
@FunctionalInterface的作用就是標(biāo)識一個接口為函數(shù)式接口,此時Comparator里只能有一個抽象方法。
使用lambda表達式之后(1)中的代碼改造如下
(2)
1
2
3
4
5
6
7
8
9
10
11
12
|
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)
1
2
3
4
5
6
7
8
|
public void testThread(){ new Thread( new Runnable() { @Override public void run() { System.out.println( "hello, i am thread!" ); } }).start(); } |
其中Runnable接口也是一個函數(shù)式接口,源碼如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
@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)
1
2
3
|
public void testThread_(){ new Thread(()-> System.out.println( "hello, i am thread!" )).start(); } |
對于沒有參數(shù)的情況 ,語法:
1.參數(shù)的括號不能省略,如果只有一句的表達式則可省略花括號和語句結(jié)尾的分號
我們構(gòu)造一個只有一個參數(shù)的函數(shù)式接口
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
@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類,
1
2
3
4
5
6
|
/** * 需要單個參數(shù) */ public static void testOnePar1(Consumer unaryOperator){ unaryOperator.accept( "msg" ); } |
總結(jié)
以上所述是小編給大家介紹的使用Java8之lambda表達式基本語法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對服務(wù)器之家網(wǎng)站的支持!
原文鏈接:http://www.cnblogs.com/andywithu/p/7344507.html