Lambda

Lambda expressions

1.1 Overview of functional programming ideas

In mathematics, a function is a set of calculation schemes with inputs and outputs, which is also called “operation with data”. Object-oriented thinking emphasizes that “things must be done in the form of objects”. Functional thinking tries to ignore the complex syntax of object-oriented thinking: “Emphasize what to do, And the Lambda expression that we’re going to look at is the idea of a function, right

1.2 Experience Lambda expressions

Example: thread creation:

public class MyRunnable implements Runnable{
	@Override
	public void run(a) {
		System.out.println("Multithreading started."); }}Copy the code
	public static void main(String[] args) {
		MyRunnable my = new MyRunnable();
		Thread t = new Thread(my);
		t.start();
}
Copy the code

———– is improved with anonymous inner classes

	public static void main(String[] args) {
		// Anonymous inner class improvements
		new Thread(new Runnable() {
			@Override
			public void run(a) {
			System.out.println("Multithreading started.");
			}
		}).start();
}
Copy the code

———– is improved with Lambda expressions

	public static void main(String[] args) {
		new Thread( () -> {
			System.out.println("Multithreading started."); }).start(); }}Copy the code

1.3 Standard format for Lambda expressions

Code analysis:

  1. (): there is no content inside, which can be regarded as method form parameter is empty
  2. ->: Use the arrow to point to the next thing to do
  3. {}: contains a piece of code, called a code block, which can be thought of as the contents of the method body

Lambda expressions are made up of three elements: formal arguments, arrows, and code blocks

The format of a Lambda expression

  1. Format: (formal argument)-> {code block}
  2. Formal parameters: If there are multiple parameters, separate them with commas. If there are no parameters, leave them blank
  3. ->: is composed of a line and a greater than symbol in English, fixed writing method. Stands for pointing action
  4. Code block: is the specific thing we are going to do, which is the method body content we wrote before

1.4 Precautions for Use

  1. To use Lambda, you must have an interface and only one abstract method in the interface
  2. Runnable r = () -> system.out.println (“hello”); Runnable r = () -> system.out.println (“hello”); New Thread(() -> system.out.println (“hello”)).start();

1.5 and anonymous inner classes

Different types are required.

  • Anonymous inner class: Can be an interface, an abstract class, or a concrete class
  • Lambda expressions: Can only be interfaces

Different use restrictions

  • If there is one and only one abstract method in the interface, Lambda expressions can be used, or anonymous inner classes can be used
  • If there is more than one abstract method in the interface, only anonymous inner classes can be used, not Lambda expressions

  • Anonymous inner class: When compiled, a separate. Class bytecode file is generated
  • Lambda expressions: After compilation, there is no single. Class bytecode file. The corresponding bytecode is dynamically generated at run time