java複習第7天---7.2----Lambda表達式

java複習第7天---7.2----Lambda表達式


目錄




內容

2、Lambda表達式

2.1、產生背景

  • 函數式編程思想:有輸入和輸出的一套算法,即"拿什麼東西多什麼"。強調做什麼,而不是怎麼做

  • 面向對象編程思想:強調通過對象形式做什麼

  • 冗餘的Runnable代碼

      package thread.pool;
    
      public class TestThread1 {
      	public static void main(String[] args) {
      		new Thread(new Runnable() {
    
      			@Override
      			public void run() {
      				System.out.println("多線程任務");			
      			}
    
      		}).start();
      	}
      }
    
  • 分析:

    • 匿名內部類的好處和弊端:一方面匿名內部類,省去實現類的定義;另一方面,匿名內部類語法–很複雜
    • 語法分析
      • Runnable接口只有一個run方法
      • 無參數
      • 無返回值
      • 代碼塊(方法體):邏輯任務
  • 結論:我們只想關心任務邏輯部分,不想關心其他部分。如現實生活中我們想去北京,我們不關心做什麼交通工具去,只要到北京就可以。

  • Lambda語法

    • () -> System.out.println(“多線程任務”);
    • () : 對應run中的參數
    • -> :表示前面參數傳遞給後面的代碼塊
    • 後面語句: 業務邏輯

2.2、標準格式

	(參數列表) -> { 任務代碼塊 }
  • 參數列表: 參數

  • -> : 表示前面參數傳遞給後面

  • {} : 表示業務邏輯部分

    • 當{}內只有一條語句的時候,{}可以省略
    • 當只有一條return 語句的時候,return 可以省略
  • 示例2.2-1:lambda多線程實現

      package thread.lambda;
    
      public class TestLambda1 {
      	public static void main(String[] args) {
      		new Thread(() -> System.out.println("多線程任務")).start();
      	}
      }
      測試結果:
      多線程任務
    

2.3、無參無返回值

  • 示例2.3-1:同上

2.4、有參有返回值

package thread.lambda;

public interface Calculator {
	int add(int a, int b); // 返回 a + b 的值
}

package thread.lambda;

public class TestCalculator {
	public static void main(String[] args) {
		invokeCalc(3, 4, (int a, int b) -> a + b);
	}

	public static void invokeCalc(int a, int b, Calculator calculator) {
		int result = calculator.add(a, b);
		System.out.println(a + "+" + b + "=" + result);
	}
}
測試結果:
3+4=7

2.5、省略規則

  • 小括號內參數類型可以省略
  • 如果小括號內有且僅有一個參數,小括號可以省略
  • 如果大括號內有且僅有一個參數,無論是否有返回值,都可以省略大括號、return關鍵字及語句分號

2.5、使用限制

  1. 使用Lambda語法必須有接口,且要求接口中有且僅有1個抽象方法:無論java內置Runnable,Comparator或者自定義的接口,只有當接口中抽象方法存在且唯一時,纔可以使用Lambda接口

  2. 使用Lambda語句必須有上下文推斷:方法的參數或者局部變量類型必須爲Lambda對應的類型,才能使用Lambda作爲該接口的實例

3、案例

  1. 自定義學生類型,根據年齡升序排序

     package array.sort;
    
     public class Student {
     	private String name;
     	private int age;
     	private int score;
     	public Student(String name, int age, int score) {
     		super();
     		this.name = name;
     		this.age = age;
     		this.score = score;
     	}
     	public String getName() {
     		return name;
     	}
     	public void setName(String name) {
     		this.name = name;
     	}
     	public int getAge() {
     		return age;
     	}
     	public void setAge(int age) {
     		this.age = age;
     	}
     	public int getScore() {
     		return score;
     	}
     	public void setScore(int score) {
     		this.score = score;
     	}
    
     	@Override
     	public String toString() {
     		return "Student [name=" + name + ", age=" + age + ", score=" + score + "]";
     	}
    
    
    
     }
     
     package array.sort;
    
     import java.util.Arrays;
     import java.util.Comparator;
    
     public class TestStudent1 {
     	public static void main(String[] args) {
     		Student s1 = new Student("趙剛", 18, 51);
     		Student s2 = new Student("王雪", 20, 95);
     		Student s3 = new Student("張小強", 21, 60);
     		Student s4 = new Student("小麗", 22, 77);
    
     		Student[] arr = new Student[4];
     		arr[0] = s4;
     		arr[1] = s2;
     		arr[2] = s3;
     		arr[3] = s1;
    
     		System.out.println(Arrays.toString(arr));
     		Arrays.sort(arr, (o1, o2) -> o1.getAge() - o2.getAge());
    
     		System.out.println(Arrays.toString(arr));
    
     	}
     }
     測試結果:
     [Student [name=小麗, age=22, score=77], Student [name=王雪, age=20, score=95], Student [name=張小強, age=21, score=60], Student [name=趙剛, age=18, score=51]]
     [Student [name=趙剛, age=18, score=51], Student [name=王雪, age=20, score=95], Student [name=張小強, age=21, score=60], Student [name=小麗, age=22, score=77]]
    

後記

本項目爲參考某馬視頻開發,相關視頻及配套資料可自行度娘或者聯繫本人。上面爲自己編寫的開發文檔,持續更新。歡迎交流,本人QQ:806797785

前端項目源代碼地址:https://gitee.com/gaogzhen/vue-leyou
    後端JAVA源代碼地址:https://gitee.com/gaogzhen/JAVA

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章