lambda 表達(dá)式的基礎(chǔ)語法: Java 8 中引入了操作符 -> 箭頭操作 lambda 操作符
箭頭操作符就lambda表達(dá)式拆分為兩部分:
左側(cè):lambda 表達(dá)式的參數(shù)列表
右側(cè):lambda 表達(dá)式所需要執(zhí)行的功能传货, lambda體
語法格式1: 無參數(shù)站蝠,無返回值
Runnabler=()->{
System.out.println("hello java");
? };
語法格式2 : 一個參數(shù),無返回值
//對抽象方法的實現(xiàn)? 接口只有一個方法
Consumer<String>con=(x)->System.out.println(x);
con.accept("hello java");
語法格式3 : 有一個參數(shù)样眠,,小括號可以不寫
語法格式4 : 有兩個及以上的參數(shù)咸灿,并且lambda體中有多條語句摸吠,并且有返回值
Comparator<Integer>com=(x,y)->{
System.out.println("hello Java");
returnInteger.compare(x,y);
};
5 : 若lambda體中只有一條語句丛塌, return 和大括號都可以不寫
Comparator<Integer>com=(x,y)->Integer.compare(x,y);
6 : lambda 表達(dá)式的參數(shù)列表的數(shù)據(jù)類型可以不寫较解,jvm編譯器可以通過上下文推斷出,數(shù)據(jù)類型赴邻, 類型推斷
二 Lambda 表達(dá)式需要函數(shù)式接口的支持 ?
函數(shù)式接口: 接口中只有一個抽象方法的接口印衔,稱為函數(shù)式接口,可以使用注解? @FunctionalInterface 修飾 可以檢查是否是函數(shù)式接口
java 8 中內(nèi)置的四大核心函數(shù)式接口
Consumer<T> :? 消費性接口* ? ?
? void accept(T t)
Supplier<T> : 供給型接口 ? ?
? ? T get();
Function<T, R> : 函數(shù)型接口
?? R apply(T t)
Predicate<T> :? 斷言型接口* ?
? boolean? test(T t)
//consumer 消費性接口@Test
publicvoidtest1() {
happy(10000, (x)->{
System.out.println("消費了"+x+"元");
?? });
}
publicvoidhappy(doublemoney,Consumer<Double>con) {
con.accept(money);
}
//supplier<T> 供給型接口
@Test
publicvoidtest2() {
List<Integer>numbers=getNumbers(10, ()->(int)(Math.random()*100));System.out.println(numbers);
}//產(chǎn)生一些整數(shù)姥敛,并放入集合中
publicList<Integer>getNumbers(intnum,Supplier<Integer>sup) {ArrayList<Integer>list=newArrayList<>();
for(inti=0;i<num;i++) {
Integern=sup.get();
list.add(n);
? ? ? ? } ? ?
returnlist;
? ? }
//Function<T, R> : 函數(shù)型接口
//需求:用于處理字符串
publicStringstrHandler(Stringstr,Function<String,String>fun) {
returnfun.apply(str);
?? }
@Test
publicvoidtest3() {
StringnewStr=strHandler("/t/t/t hallo Java ? ", (str)->str.trim());
System.out.println(newStr);
?? }
方法引用
方法引用 : 若lambda 體中的內(nèi)容有方法已經(jīng)實現(xiàn)了奸焙,我們可以使用方法引用
* ? ? ? (可以理解為方法引用是lambda 表達(dá)式的另外一種表現(xiàn)形式)
* 主要有三種語法格式
* 對象::實例方法名
* 類::靜態(tài)方法名
* 類::實例方法名
?
* 注意:Lambda 體中調(diào)用方法的參數(shù)列表與返回值類型,要與函數(shù)式接口中抽象方法的函數(shù)列表和返回值類型保持一致彤敛。
* ? ? ? 若lambda 參數(shù)列表中的第一個參數(shù)是實例方法的調(diào)用者与帆,第二個參數(shù)是實例方法的參數(shù)時,樂意使用CLassName::method
//對象::實例方法名
@Test
publicvoidtest1() {
Consumer<String>con=(x)->System.out.println(x);
PrintStreamps=System.out;
Consumer<String>con1=ps::println;
con1.accept("acvd");
?? }
@Test
publicvoidtest2() {
Employeeemp=newEmployee(111,"zs",12,1000);
Supplier<String>sup=()->emp.getName();
Stringstr=sup.get();
?
Supplier<Integer>sup2=emp::getAge;
Integerinteger=sup2.get();
System.out.println(integer);
?? }
//類::靜態(tài)方法名
@Test
publicvoidtest3() {
Comparator<Integer>com=(x,y)->Integer.compare(x,y);
?
Comparator<Integer>com1=Integer::compare;
?? }
?
//類::實例方法名
@Test
publicvoidtest4() {
BiPredicate<String,String>bp=(x,y)->x.equals(y);
?
BiPredicate<String,String>bp2=String::equals;
?? }
二 : 構(gòu)造器引用:
*
*? 格式:
*
*? ClassName::new
*
* 注意: 需要調(diào)用的構(gòu)造器的參數(shù)列表與函數(shù)接口中抽象方法的參數(shù)列表保持一致
//構(gòu)造器引用
@Test
publicvoidtest5() {
Supplier<Employee>sup=()->newEmployee();
?
//構(gòu)造器引用方式? 午餐
Supplier<Employee>sup2=Employee::new;
Employeeemployee=sup2.get();
System.out.println(employee);
?? }
注意: 需要調(diào)用的構(gòu)造器的參數(shù)列表與函數(shù)接口中抽象方法的參數(shù)列表保持一致
*
* 三 數(shù)組引用
*
* Type :: new
*
//數(shù)組引用
@Test
publicvoidtest6() {
Function<Integer,String[]>fun=(x)->newString[x];
String[]apply=fun.apply(10);
System.out.println(apply.length);
?
Function<Integer,String[]>fun2=String[]::new;
String[]apply1=fun2.apply(20);
System.out.println(apply1.length);
?? }