java8新特性專題之二、lambda基礎語法
阿新 • • 發佈:2022-03-06
幾種基礎語法
一、Lambda 表示式的基礎語法:Java8中引入了一個新的操作符 "->" 該操作符稱為箭頭操作符或 Lambda 操作符
箭頭操作符將 Lambda 表示式拆分成兩部分:
左側:Lambda 表示式的引數列表
右側:Lambda 表示式中所需執行的功能, 即 Lambda 體
語法
1. 語法格式一:無引數,無返回值 () -> System.out.println("Hello Lambda!"); 2. 語法格式二:有一個引數,並且無返回值 (x) -> System.out.println(x)3. 語法格式三:若只有一個引數,小括號可以省略不寫 x -> System.out.println(x) 4. 語法格式四:有兩個以上的引數,有返回值,並且 Lambda 體中有多條語句 Comparator<Integer> com = (x, y) -> { System.out.println("函式式介面"); return Integer.compare(x, y); }; 5. 語法格式五:若 Lambda 體中只有一條語句, return 和 大括號都可以省略不寫 Comparator<Integer> com = (x, y) -> Integer.compare(x, y); 6. 語法格式六:Lambda 表示式的引數列表的資料型別可以省略不寫,因為JVM編譯器通過上下文推斷出,資料型別,即“型別推斷” (Integer x, Integer y) -> Integer.compare(x, y);
Lambda 表示式需要“函式式介面”的支援
函式式介面:介面中只有一個抽象方法的介面,稱為函式式介面。 可以使用註解 @FunctionalInterface 修飾 可以檢查是否是函式式介面
@FunctionalInterface public interface MyFun { public Integer getValue(Integer xy); } public static Integer getFunValue(Integer num,MyFun fun){ return fun.getValue(num); } public static void m2(){ System.out.println(getFunValue(12,x-> {return x + 10;})); }
四大核心函式式介面
- 消費性介面:
public static void test1(){ consum(10,(x)->System.out.println("消費了: "+x)); } //Consumer<T> 消費型介面 : public static void consum(int m, Consumer<Integer> consumer){ consumer.accept(m); }
- 供給型介面
public static void test2(){ int value = supplier(11,()-> 11*11); System.out.println(value); } //供給型介面 public static int supplier(int n, Supplier<Integer> supplier){ return supplier.get(); }
- 函式型介面
public static void test3(){ function("test",(x)->{return x.length();}); } //函式型介面 public static Integer function(String str, Function<String,Integer> function){ return function.apply(str); }
- 斷言型介面
public static void test4(){ boolean result = predicate("123qwe",(x)->x.equals("123qwe")); System.out.println(result); } //斷言型介面 public static boolean predicate(String str, Predicate<String> predicate){ return predicate.test(str); }
例子
package com.zs.boot.controller; import java.util.ArrayList; import java.util.Comparator; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.function.Consumer; import org.junit.Test; /* * 一、Lambda 表示式的基礎語法:Java8中引入了一個新的操作符 "->" 該操作符稱為箭頭操作符或 Lambda 操作符 * 箭頭操作符將 Lambda 表示式拆分成兩部分: * * 左側:Lambda 表示式的引數列表 * 右側:Lambda 表示式中所需執行的功能, 即 Lambda 體 * * 語法格式一:無引數,無返回值 * () -> System.out.println("Hello Lambda!"); * * 語法格式二:有一個引數,並且無返回值 * (x) -> System.out.println(x) * * 語法格式三:若只有一個引數,小括號可以省略不寫 * x -> System.out.println(x) * * 語法格式四:有兩個以上的引數,有返回值,並且 Lambda 體中有多條語句 * Comparator<Integer> com = (x, y) -> { * System.out.println("函式式介面"); * return Integer.compare(x, y); * }; * * 語法格式五:若 Lambda 體中只有一條語句, return 和 大括號都可以省略不寫 * Comparator<Integer> com = (x, y) -> Integer.compare(x, y); * * 語法格式六:Lambda 表示式的引數列表的資料型別可以省略不寫,因為JVM編譯器通過上下文推斷出,資料型別,即“型別推斷” * (Integer x, Integer y) -> Integer.compare(x, y); * 例如:jdk1.8中 * //下面也是型別推斷 * List<String> list = new ArrayList<>(); * * //直接這樣呼叫show,這樣也是型別推斷 * show(new HashMap<>()); * public void show(Map<String, Integer> map){ * * } * * 上聯:左右遇一括號省 * 下聯:左側推斷型別省 * 橫批:能省則省 * * 二、Lambda 表示式需要“函式式介面”的支援 * 函式式介面:介面中只有一個抽象方法的介面,稱為函式式介面。 可以使用註解 @FunctionalInterface 修飾 * 可以檢查是否是函式式介面 */ public class TestLambda2 { @Test public void test1(){ int num = 0;//jdk 1.7 前,必須是 final。 // 因為在JDK8之前,如果我們在匿名內部類中需要訪問區域性變數,那麼這個區域性變數必須用final修飾符修飾。 Runnable r = new Runnable() { @Override public void run() { System.out.println("Hello World!" + num); } }; r.run(); System.out.println("-------------------------------"); Runnable r1 = () -> System.out.println("Hello Lambda!"); r1.run(); } @Test public void test2(){ Consumer<String> con = x -> System.out.println(x); con.accept("helloworld!"); } @Test public void test3(){ Comparator<Integer> com = (x, y) -> { System.out.println("函式式介面"); return Integer.compare(x, y); }; } @Test public void test4(){ Comparator<Integer> com = (x, y) -> Integer.compare(x, y); } //需求:對一個數進行運算 @Test public void test6(){ Integer num = operation(100, (x) -> x*x); System.out.println(num); System.out.println(operation(200, (y) -> y + 200)); } public Integer operation(Integer num, MyFun mf){ return mf.getValue(num); } @FunctionalInterface public interface MyFun public Integer getValue(Integer num); } }
例子
package com.atguigu.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; /* * Java8 內建的四大核心函式式介面 * * Consumer<T> : 消費型介面 * void accept(T t); * * Supplier<T> : 供給型介面 * T get(); * * Function<T, R> : 函式型介面 * R apply(T t); * * Predicate<T> : 斷言型介面 * boolean test(T t); * */ public class TestLambda3 { //Predicate<T> 斷言型介面: @Test public void test4(){ List<String> list = Arrays.asList("Hello", "atguigu", "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> 函式型介面: @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); } } //需求:產生指定個數的整數,並放入集合中 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> 消費型介面 : @Test public void test1(){ happy(10000, (m) -> System.out.println("你們剛哥喜歡大寶劍,每次消費:" + m + "元")); } public void happy(double money, Consumer<Double> con){ con.accept(money); } }
參考:https://www.jianshu.com/p/d70300190435