Lambda 運算式
? ? ? ? ? ? ? ? Lambda 運算式的實質屬于函式式編程,
? ? ? ? ? ? ? ? 語法格式為:(parameters) -> expression或(parameters) ->{statements; }
? ? ? ? ? ? ? ?
Lambda 運算式的特點
- Lambda 運算式只能參考標記了 final 的外層區域變數
- 不能在lambda 內部修改定義在域外的區域變數
- Lambda 運算式的區域變數可以不用宣告為 final,但是必須不可被后面的代碼修改(自帶 final)
- Lambda 運算式當中不允許宣告一個與區域變數同名的引數或者區域變數
? ? ? ? ? ? ? ?
Lambda 運算式的作用
- 避免匿名內部類定義過多
- 讓代碼更簡潔,只留下核心邏輯
? ? ?
函式式介面
? ? ? ? ? ? ? ? 函式式介面 Functional Interface,是只包含唯一一個抽象方法的介面,對于函式式介面,可以通過 Lambda 運算式來直接創建該介面的物件,如 Runnable 介面:
public interface Runnable {
public abstract void run();
}
使用 Lambda 運算式實作 Runnable
//常規寫法
new Thread(new Runnable(){
@Override
public void run(){
...
}
}).start();
//Lambda 運算式
new Thread( () -> {
...
}).start();
? ? ? ? ? ? ? ?
JDK 1.8之前已有的函式式介面:
- java.lang.Runnable
- java.util.concurrent.Callable
- java.security.PrivilegedAction
- java.util.Comparator
- java.io.FileFilter
- java.nio.file.PathMatcher
- java.lang.reflect.InvocationHandler
- java.beans.PropertyChangeListener
- java.awt.event.ActionListener
- javax.swing.event.ChangeListener
JDK 1.8 新增加的函式介面:
- java.util.function
? ? ? ? ? ? ? ?
Lambda 運算式的簡化程序
? ? ? ? ? ? ? ? 實作類-->靜態內部類-->區域內部類-->匿名內部類-->Lambda 運算式
? ? ? ? ? ? ? ? 簡化程序如下:
實作類
public class TestLamda01 {
public static void main(String[] args) {
DoSports jog = new Jog();
}
}
interface DoSports{
void start();
}
//實作類
class Jog1 implements DoSports{
@Override
public void start() {
System.out.println("I'm jogging");
}
}
靜態內部類
public class TestLambda01 {
//靜態內部類
static class Jog implements DoSports{
public void start(){
System.out.println("I'm jogging");
}
}
public static void main(String[] args) {
DoSports jog = new Jog();
jog.start();
}
}
interface DoSports{
void start();
}
區域內部類
public class TestLambda01 {
public static void main(String[] args) {
//區域內部類
class Jog implements DoSports{
public void start(){
System.out.println("I'm jogging");
}
}
DoSports jog = new Jog();
jog.start();
}
}
interface DoSports{
void start();
}
匿名內部類
public class TestLambda01 {
public static void main(String[] args) {
DoSports jog = new DoSports(){
public void start(){
System.out.println("I'm jogging");
}
};
jog.start();
}
}
interface DoSports{
void start();
}
? ? ? ? ? ? ? ?
Lambda 運算式
public class TestLambda01 {
public static void main(String[] args) {
DoSports jog = ()->{
System.out.println("I'm jogging"); //start() 方法執行主體
};
jog.start();
}
}
interface DoSports{
void start();
}
含引數的 Lambda 運算式簡化
? ? ? ? ? ? ? ? 當只有一個引數時,可簡化引數型別和括號
public class TestLambda01 {
public static void main(String[] args) {
/*可簡化引數型別和括號
DoSports jog = (int a)->{
...
};*/
DoSports jog = a->{
//start() 方法執行主體
...
};
jog.start(引數1);
}
}
interface DoSports{
void start(int a);
}
? ? ? ? ? ? ? ? 當有多個引數時,可簡化引數型別,必須加括號
public class TestLambda01 {
public static void main(String[] args) {
/*可簡化引數型別和括號
DoSports jog = (int a,String b)->{
...
};*/
DoSports jog = (a,b)->{
//start() 方法執行主體
...
};
jog.start(引數1,引數2);
}
}
interface DoSports{
void start(int a,String b);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/310362.html
標籤:其他
