Java8新特性:lambda表達(dá)式總結(jié)
Lambda 表達(dá)式的基礎(chǔ)語法:Java8中引入了一個新的操作符 '->' 該操作符稱為箭頭操作符或 Lambda 操作符箭頭操作符將 Lambda 表達(dá)式拆分成兩部分:
左側(cè):Lambda 表達(dá)式的參數(shù)列表 右側(cè):Lambda 表達(dá)式中所需執(zhí)行的功能,即 Lambda 體語法格式一:無參數(shù),無返回值
() -> System.out.println('Hello Lambda!');
語法格式二:有一個參數(shù),并且無返回值
(x) -> System.out.println(x)
語法格式三:若只有一個參數(shù),小括號可以省略不寫
x -> System.out.println(x)
語法格式四:有兩個以上的參數(shù),有返回值,并且 Lambda 體中有多條語句
Comparator<Integer> com = (x, y) -> {System.out.println('函數(shù)式接口');return Integer.compare(x, y);};
語法格式五:若 Lambda 體中只有一條語句, return 和 大括號都可以省略不寫
Comparator<Integer> com = (x, y) -> Integer.compare(x, y);
語法格式六:Lambda 表達(dá)式的參數(shù)列表的數(shù)據(jù)類型可以省略不寫,因?yàn)镴VM編譯器通過上下文推斷出,數(shù)據(jù)類型,即“類型推斷”
(Integer x, Integer y) -> Integer.compare(x, y); 二、函數(shù)式接口
Lambda 表達(dá)式需要“函數(shù)式接口”的支持
函數(shù)式接口:接口中只有一個抽象方法的接口,稱為函數(shù)式接口。 可以使用注解 @FunctionalInterface 修飾用來檢查是否是函數(shù)式接口。
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);三、內(nèi)置的四大核心函數(shù)式接口用法實(shí)例
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> 斷言型接口:@Testpublic 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ù)型接口:@Testpublic void test3(){String newStr = strHandler('ttt 我叫劉亞壯 ', (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> 供給型接口 :@Testpublic void test2(){List<Integer> numList = getNumList(10, () -> (int)(Math.random() * 100));for (Integer num : numList) {System.out.println(num);}}//需求:產(chǎn)生指定個數(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)型接口 :@Testpublic 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)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. python實(shí)現(xiàn)讀取類別頻數(shù)數(shù)據(jù)畫水平條形圖案例2. python中PyQuery庫用法分享3. python操作數(shù)據(jù)庫獲取結(jié)果之fetchone和fetchall的區(qū)別說明4. JavaScript實(shí)現(xiàn)組件化和模塊化方法詳解5. 使用css實(shí)現(xiàn)全兼容tooltip提示框6. python 爬取嗶哩嗶哩up主信息和投稿視頻7. JSP+Servlet實(shí)現(xiàn)文件上傳到服務(wù)器功能8. CSS3實(shí)現(xiàn)動態(tài)翻牌效果 仿百度貼吧3D翻牌一次動畫特效9. php使用正則驗(yàn)證密碼字段的復(fù)雜強(qiáng)度原理詳細(xì)講解 原創(chuàng)10. Python編寫nmap掃描工具
