Origin of lambda expression
lambda expression is a concept introduced in jdk8, so if you want to use it
//Define an interface interface IMovingBricks{ void lambda(); }
1, External implementation class
public class TestLambda1 { public static void main(String[] args) { IMovingBricks movingBricks = new MovingBricks(); movingBricks.lambda(); } } //Implementation class class MovingBricks implements IMovingBricks{ @Override public void lambda() { System.out.println("Try to move bricks!"); } }
The external and internal forms are too troublesome. I have to create another file!
2, Static inner class
public class TestLambda1 { static class MovingBricks2 implements IMovingBricks{ @Override public void lambda() { System.out.println("Keep moving bricks!"); } } public static void main(String[] args) { IMovingBricks movingBricks = new MovingBricks2(); movingBricks.lambda(); } }
Static internal classes are troublesome, and you need to write a method outside!
3, Local inner class
public class TestLambda1 { public static void main(String[] args) { class MovingBricks3 implements IMovingBricks{ @Override public void lambda() { System.out.println("I can move!"); } } IMovingBricks movingBricks = new MovingBricks3(); movingBricks.lambda(); } }
And save!
4, Anonymous inner class
public class TestLambda1 { public static void main(String[] args) { IMovingBricks movingBricks4 = new IMovingBricks(){ @Override public void lambda() { System.out.println("I'm dying"); } }; movingBricks4.lambda(); } }
Can you save it? lambda: let me do it!
5, lambda expression
public class TestLambda1 { public static void main(String[] args) { IMovingBricks movingBricks5 = ()->{ System.out.println("I feel like I can again!"); }; } }
Can it be optimized? I'll leave it to you.
lambda is really magical, making our code more elegant;
Next, I think we should talk about how to use lambda expressions;
The interface class needs to be a functional interface; The interface class with only one method is called functional interface. Like the interface defined above, there is also the Runnable interface in multithreading;
//Define an interface interface IMovingBricks{ void lambda(); }
public interface Runnable { public abstract void run(); }
Like the Thread class Thread, there is a construction method of passing Runnable, and Runnable is a functional interface. Like the example above, it can be written as follows through lambda expression:
//A construction method of Thread class public Thread(Runnable target) { init(null, target, "Thread-" + nextThreadNum(), 0); } //new a Thread object new Thread(()->{ System.out.println("Used in thread class lambda expression"); }).start(); //Open thread
Abbreviation of lambda expression
Now our interface needs to pass an int type parameter
interface IMovingBricks{ void lambda(int k); }
- Omit parameter type:
public static void main(String[] args) { int k = 1024; IMovingBricks movingBricks5 = (k)->{ System.out.println("I feel like I can again!"+k); }; }
This is not difficult to understand, because the functional interface has only one method, and the parameters of the method are naturally easy to infer;
- If there is only one parameter, parentheses can be omitted:
public static void main(String[] args) { int k = 1024; IMovingBricks movingBricks5 = k->{ System.out.println("I feel like I can again!"+k); }; }
- If you enter only one line of code, you can omit braces
public static void main(String[] args) { int k = 1024; IMovingBricks movingBricks5 = k->System.out.println("I feel like I can again!"+k); }
summary
This paper introduces the derivation process and writing method of lambda expression, and compares the entry level;
It is often used in actual development, so we must master it!