I. Basic Introduction
-
Lambda
Lambda is a new feature introduced by java 8. A lambda expression is an anonymous function. It provides a Simpler Syntax and cooperation mode, which enables us to replace the functional interface by expression.
Lambda expressions can be used to simplify the creation of anonymous inner classes.
-
Functional interface
The so-called functional interface refers to the interface with only one abstract method
II. Lambda format
Lambda format consists of three parts: some parameters, an arrow, and a piece of code
-
The standard format is:
(parameter type parameter name) - > {code statement}
Explain:
- The syntax in parentheses is consistent with the parameter list of traditional methods. If there are no parameters, leave blank; if there are multiple parameters, separate them with commas.
- ->Is a new syntax format, representing pointing actions
- The syntax in braces is basically the same as the traditional method body requirements.
-
Omit style:
On the basis of Lambda standard format, the rules of omitting style writing are as follows:
- Parameter types in parentheses can be omitted
- If there is only one parameter in the parenthesis, the parenthesis can be omitted
- If there is only one statement in the brace, you can omit the brace, return keyword and statement semicolon, regardless of whether there is a return value or not
III. premise of Lambda
- You must have an interface to use Lambda, and you need an interface with only one abstract method. (functional interface)
- Using Lambda must have context inference. That is to say, the parameter or local variable type of the calling method must be the interface type corresponding to Lambda to use Lambda as an instance of the interface.
Four. Example
-
Basic Lambda example
Suppose you have a List, which is traversed using the for loop
import java.util.*; public class LambdaDemo { public static void main(String[] args) { String[] fruits = {"apple", "banana", "peach", "watermelon", "strawberry", "pear"}; List<String> fruitsList = Arrays.asList(fruits); // Cycles before java8 for (String f : fruitsList) { System.out.println(f + " "); } System.out.println("--------------------"); // Using lambda expressions to manipulate loops fruitsList.forEach(f -> System.out.println(f + " ")); } }
-
When implementing the Runnable interface
public class LambdaDemo { public static void main(String[] args) { // Create thread instance Thread t = new Thread(new Runnable(){ @Override public void run() { System.out.println(Thread.currentThread().getName()); } }); // Creating a Runnable interface using lambda expressions new Thread(() -> System.out.println(Thread.currentThread().getName())).start(); t.start(); System.out.println(Thread.currentThread().getName()); } }
-
Lambda sorts sets
import java.util.*; public class LambdaDemo { public static void main(String[] args) { String[] fruits = {"apple", "peach", "watermelon", "banana", "strawberry", "pear"}; // Use anonymous inner class sorting // Arrays.sort(fruits, new Comparator<String>() { // @Override // public int compare(String s1, String s2) { // return s1.compareTo(s2); // } // }); // Sorting with lambda expressions Arrays.sort(fruits, (s1, s2) -> s1.compareTo(s2)); System.out.println(Arrays.toString(fruits)); } }