一、Lambda 表達(dá)式的基礎(chǔ)語(yǔ)法
Lambda 表達(dá)式的基礎(chǔ)語(yǔ)法:Java8中引入了一個(gè)新的操作符 "->" 該操作符稱為箭頭操作符或 Lambda 操作符箭頭操作符將 Lambda 表達(dá)式拆分成兩部分:
- 左側(cè):Lambda 表達(dá)式的參數(shù)列表
- 右側(cè):Lambda 表達(dá)式中所需執(zhí)行的功能,即 Lambda 體
語(yǔ)法格式一:無(wú)參數(shù),無(wú)返回值
1
|
() -> System.out.println( "Hello Lambda!" ); |
語(yǔ)法格式二:有一個(gè)參數(shù),并且無(wú)返回值
1
|
(x) -> System.out.println(x) |
語(yǔ)法格式三:若只有一個(gè)參數(shù),小括號(hào)可以省略不寫(xiě)
1
|
x -> System.out.println(x) |
語(yǔ)法格式四:有兩個(gè)以上的參數(shù),有返回值,并且 Lambda 體中有多條語(yǔ)句
1
2
3
4
|
Comparator<Integer> com = (x, y) -> { System.out.println( "函數(shù)式接口" ); return Integer.compare(x, y); }; |
語(yǔ)法格式五:若 Lambda 體中只有一條語(yǔ)句, return 和 大括號(hào)都可以省略不寫(xiě)
1
|
Comparator<Integer> com = (x, y) -> Integer.compare(x, y); |
語(yǔ)法格式六:Lambda 表達(dá)式的參數(shù)列表的數(shù)據(jù)類型可以省略不寫(xiě),因?yàn)镴VM編譯器通過(guò)上下文推斷出,數(shù)據(jù)類型,即“類型推斷”
1
|
(Integer x, Integer y) -> Integer.compare(x, y); |
二、函數(shù)式接口
Lambda 表達(dá)式需要“函數(shù)式接口”的支持
函數(shù)式接口:接口中只有一個(gè)抽象方法的接口,稱為函數(shù)式接口。 可以使用注解 @FunctionalInterface 修飾用來(lái)檢查是否是函數(shù)式接口。
Java8 內(nèi)置的四大核心函數(shù)式接口
1
2
3
4
5
6
7
8
9
10
11
|
Consumer<T> : 消費(fèi)型接口 void accept(T t); Supplier<T> : 供給型接口 T get(); Function<T, R> : 函數(shù)型接口 R apply(T t); Predicate<T> : 斷言型接口 boolean test(T t); |
三、內(nèi)置的四大核心函數(shù)式接口用法實(shí)例
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
|
package com.lyz.java8; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.function.Consumer; import java.util.function.Function; import java.util.function.Predicate; import java.util.function.Supplier; import org.junit.Test; /* * @author liuyazhuang * @version 1.0.0 * @date 2018/8/19 15:02 * @description Java8 內(nèi)置的四大核心函數(shù)式接口 * * Consumer<T> : 消費(fèi)型接口 * void accept(T t); * * Supplier<T> : 供給型接口 * T get(); * * Function<T, R> : 函數(shù)型接口 * R apply(T t); * * Predicate<T> : 斷言型接口 * boolean test(T t); * */ public class TestLambda { //Predicate<T> 斷言型接口: @Test public void test4(){ List<String> list = Arrays.asList( "Hello" , "world" , "Lambda" , "www" , "ok" ); List<String> strList = filterStr(list, (s) -> s.length() > 3 ); for (String str : strList) { System.out.println(str); } } //需求:將滿足條件的字符串,放入集合中 public List<String> filterStr(List<String> list, Predicate<String> pre){ List<String> strList = new ArrayList<>(); for (String str : list) { if (pre.test(str)){ strList.add(str); } } return strList; } //Function<T, R> 函數(shù)型接口: @Test public void test3(){ String newStr = strHandler( "\t\t\t 我叫劉亞壯 " , (str) -> str.trim()); System.out.println(newStr); String subStr = strHandler( "我叫劉亞壯" , (str) -> str.substring( 2 , 5 )); System.out.println(subStr); } //需求:用于處理字符串 public String strHandler(String str, Function<String, String> fun){ return fun.apply(str); } //Supplier<T> 供給型接口 : @Test public void test2(){ List<Integer> numList = getNumList( 10 , () -> ( int )(Math.random() * 100 )); for (Integer num : numList) { System.out.println(num); } } //需求:產(chǎn)生指定個(gè)數(shù)的整數(shù),并放入集合中 public List<Integer> getNumList( int num, Supplier<Integer> sup){ List<Integer> list = new ArrayList<>(); for ( int i = 0 ; i < num; i++) { Integer n = sup.get(); list.add(n); } return list; } //Consumer<T> 消費(fèi)型接口 : @Test public void test1(){ happy( 10000 , (m) -> System.out.println( "每次消費(fèi):" + m + "元" )); } public void happy( double money, Consumer<Double> con){ con.accept(money); } } |
到此這篇關(guān)于Java8新特性:lambda表達(dá)式總結(jié)的文章就介紹到這了,更多相關(guān)Java lambda表達(dá)式內(nèi)容請(qǐng)搜索服務(wù)器之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持服務(wù)器之家!
原文鏈接:https://blog.csdn.net/l1028386804/article/details/81837489