JDK 8 to the LAMBDA function programming support, shallow is simply to introduce some new grammatical structure, is following JDK5 introduction of generics after another one to everyone encoding way of an innovation, if you do not follow, I am afraid, over time, you will think that Java code is the Martian language. Deep down, Java is parallel processing in a language-level environment that further supports multi-core CPUs, as evidenced in the stream API, before Java, where there are already many mainstream languages, like C # and C + +, that support lambda function programming, This time the Java introduction of lambda support is also known after the sleep.
To write a lambda code in Java, there are two things to know. One is a lambda expression. The other is the function interface. This article is mainly for these two points to unfold.
- Lambda expression-a lambda expression is actually an anonymous method. This method cannot be executed by itself, it can only be used to implement a method defined in a function interface, and an anonymous class is produced when a lambda expression implements a function interface.
- function interface-A function interface is an interface that contains and contains only an abstract method, and the methods defined in the interface are abstract methods before JDK 8. After JDK 8, the methods defined in the interface can define a default implementation for an interface method using the default keyword. Once the default implementation is defined, it cannot be called an abstract method. The runnable,comparable interface defined before Java can be called a function interface. A lambda expression that is out of the function interface in Java can only be counted as an expression that cannot be executed. Until a lambda expression is used to implement a function interface, it can be run in the Java world.
Let's combine the function interface to parse the lambda expression.
Lambda expressions can be represented as simple expressions (p1,p2,....) | block of code
One is the LAMBDA operator, and the left side is the list of arguments required by the expression, separated by commas, or an empty parenthesis () if there are no arguments. The right side is the exact content of the expression, which is divided into simple expressions and blocks of code that contain complex logic. A simple expression can contain only a single expression, but a block of code is an anonymous block of code that can contain more complex logic.
Let's take a look at two examples of simple expressions.
(), "Hello, Lambda"
This simple example does not accept any arguments, so there is only one parenthesis on the left. On the right is an expression that returns a simple string. Similar to the following Java code we wrote earlier
String SayHello () { Return "Hello, Lambda";}
The first example of a function interface combined with a lambda expression is capable of running.
Interface Hello4lambda {public String SayHello ();} public class Hellolambda {public static void Main (string[] args) { hello4lambda hl = () "Hello, Lambda. "; System.out.println (Hl.sayhello ());} }
Let's look at an example with a parameter
(name), "Hello," + Name
This lambda expression is equivalent to the following Java code
String SayHello (string name) { Return "Hello," + name;}
The code that can be run is as follows
Interface Hello4anyone {public string SayHello (string name);} public class Helloanyone {public static void Main (string[] args) { hello4anyone hw = (name), "Hello , "+ Name; System.out.println (Hw.sayhello ("Matt"));} }
You may find that the parameters in the lambda expression do not specify the type of the parameter. Java automatically pushes the parameter type according to the function interface implemented by the lambda expression. For a simple expression, you must return the result of the operation by default. This means that the implemented function interface must have a return value and cannot be void. The following code will report a compilation error because the interface definition method is an interface method that does not have a return value.
Interface Hello4anyone {public void SayHello (String name);} public class Helloanyone {public static void Main (string[] args) { hello4anyone hw = (name), " Hello, "+ name; System.out.println (Hw.sayhello ("Matt"));} }
Let's take a look at an example with a default implementation where a compile error occurs because the interface we define is not a function interface, and there is no abstract method.
Interface Hello4anyone {public Default string SayHello (string name) {//default implementation return "Hello," + name;} ;} public class Helloanyone {public static void Main (string[] args) { hello4anyone hw = (name), "Hello , "+ name;//compilation error occurred System.out.println (Hw.sayhello (" Matt "));} }
Complex blocks of code in lambda expressions allow us to perform more complex operations than simple expressions, and we just need to place the code in {} and return the results with the return keyword in the final display of the code, which is, of course, the case where you really need to return the results. If the function interface you are trying to implement does not need to return any return values, then your lambda code block will not need to return any results.
Interface Doublecalculate { double Sum (double[] arr);} public class Doublecalculator {public static void Main (string[] args) { double[] a = new double[10]; for (int i = 0;i< a.length;i++) a[i] = math.random () * Doublecalculate dc = (arr), { double sum = 0; for (int j = 0;j<arr.length;j++) { sum + = Arr[j]; } return sum; }; System.out.println (DC. Sum (a));} }
This example sums the elements in a double array. We use the code block of the lambda expression to implement the logic. But if we try to sum the INT array, the interface Doublecalculate cannot receive an array of type int. To do this, we may also define an interface function specifically for an array of type int.
Interface Intcalculate { int Sum (int[] arr);} public class Intcalculator {public static void Main (string[] args) { int[] a = new int[10]; for (int i = 0;i< a.length;i++) a[i] = (int) (Math.random () * +); Intcalculate dc = (arr), { int sum = 0; for (int j = 0;j<arr.length;j++) { sum + = Arr[j]; } return sum; }; System.out.println (DC. Sum (a));}}
This makes the code redundant, can we use the paradigm to model the parameters of the abstract method in the function interface, so that it doesn't work? The answer is yes, the support of JDK 5 paradigm can also be applied on the function interface.
Interface Calculate<t> {T Sum (t[] arr);} public class Genericcalculator {public static void main (string[] args) {//for double array. Double[] A = new double[10]; for (int i = 0;i< a.length;i++) a[i] = math.random () * 100; calculate<double> dc = (arr), {Double sum = 0; for (int j = 0;j<arr.length;j++) {sum + = arr[j]; } return sum; }; System.out.println (DC. Sum (a)); The for int array. Integer[] B = new INTEGER[10]; for (int i = 0;i< b.length;i++) b[i] = (int) (Math.random () * 100); calculate<integer> IC = (arr), {int sum = 0; for (int j = 0;j<arr.length;j++) {sum + = arr[j]; } return sum; }; SYSTEM.OUT.PRINTLN (IC. Sum (b)); }}
But for the lambda expression itself, the paradigm is not supported, and we see two different lambda expressions for double and integer blocks of code, respectively.
The magical journey of Java Lambda expressions