Functional programming ideas Overview
In mathematics, there is a function of the amount of input, output of a calculation program, which is "to do something to get something." In contrast, the object-oriented too much emphasis on "the object must be done in the form of things," and try to ignore the idea of functional object-oriented complex syntax - emphasize what to do, rather than what form do.
Object-oriented thinking: do one thing, looking for a target to solve this matter, call the object's method to get things done.
Functional programming ideas: as long as get to the results, who do, how to do is not important, important is the result, not pay attention to the process
Redundant codes Runnable
Traditional writing: When you need to start a thread to complete the task, the task is usually defined by the content java.lang.Runnable interface and uses java.lang.Thread class to start the thread
. 1 Package demosummary.lambda; 2 . 3 public class RunnableDemo1 { . 4 public static void main (String [] args) { . 5 // anonymous inner classes . 6 the Runnable Task = new new the Runnable () { . 7 @Override . 8 public void RUN () { . 9 System.out.println ( "multithreaded execution" ); 10 } . 11 }; 12 is new new the thread (task) .start (); // start threads 13 } 14 }
Analysis of the above code:
1.Thread class needs Runnable interface as an argument, which is the core of the abstract run method used to specify the content of the task thread
2. In order to run the method specified body need have Runnable interface implementation class
3. In order to save the trouble to define a RunnableImpl implementation class, had to use anonymous inner classes;
4. The abstract must overwrite the run method, the method name, method parameters, return value of the method had to write again, and can not be wrong;
5. In fact, it seems that only the method body is the key
lambda better wording
. 1 Package demosummary.lambda; 2 . 3 / ** . 4 * the Lambda written . 5 * / . 6 public class LambdaDemo1 { . 7 public static void main (String [] args) { . 8 new new the Thread (() -> System.out.println ( " multithreaded execution ")) start ();. // start the thread 9 } 10 }
The code above and on the implementation of a code is the same effect, lambda wording needs to be compiled in order to pass more than 1.8
Recalling the anonymous inner class
1. Use the implementation classes
To start a thread, we need to create a Thread class object and call start method. In order to specify the content of the thread execution, call the Thread class constructor: public Thread (Runnable target)
In order to obtain an object that implements Runnable interface may implement a class interface definition for RunnableImpl:
. 1 Package demosummary.lambda; 2 . 3 public class RunnableImpl the implements the Runnable { . 4 @Override . 5 public void RUN () { . 6 System.out.println ( "multithreaded execution" ); 7 } 8 }
Then create an object of the implementation class as the Thread class constructor parameters:
. 1 Package demosummary.lambda; 2 . 3 public class RunnableImplTest { . 4 public static void main (String [] args) { . 5 // Create RunnableImpl objects . 6 RunnableImpl RI = new new RunnableImpl (); . 7 // start threads . 8 new new the Thread (RI) .start (); . 9 } 10 }
2. Using an anonymous inner classes
This RunnableImpl class just to achieve Runnable interface exists, and only used the only time, so the use of anonymous inner class syntax to omit a separate class definition, that anonymous inner classes:
. 1 Package demosummary.lambda; 2 . 3 / ** . 4 * using anonymous inner classes . 5 * / . 6 public class RunnableImplInit { . 7 public static void main (String [] args) { . 8 new new the Thread ( new new the Runnable () { . 9 @Override 10 public void RUN () { . 11 System.out.println ( "multithreaded execution" ); 12 is } 13 is .}) Start (); 14 } 15 }
Analysis of the above-described method of anonymous inner classes
Careful analysis of the semantic tags, Runnable interface has a run method definition: public abstract void run ();
No argument: without any conditions to implement the program.
None Return Value: This scheme does not produce any results.
Code block (Method): Any specific implementation of the program steps
The use lambda syntax to be more straightforward:
() -> System.out.println ( "multithreaded execution!")
Front of a pair of parentheses i.e. run method parameters (no), who do not need any condition;
Intermediate arrows represent a parameter to be passed in front of the code behind;
I.e. behind the business logic code output statement.
The benefits and drawbacks of an anonymous inner class
On the one hand, an anonymous inner classes can help us achieve omit the definition of the class; on the other hand, the syntax of anonymous inner classes - indeed too complicated
Lambda expressions standard format
Lambda omitted object-oriented rules, and format consists of three parts:
Some parameters 1. 2. 3. a piece of code arrow
Lambda expressions standard format for :( Parameter Type name) -> {statement} Code
Format Description:
The syntax and parameters in the conventional method 1. parentheses listing consistent: no parameter is blank;
2. The plurality of parameters are separated by commas. -> is the new syntax introduced, on behalf of pointing action.
Syntax body in the conventional method in claim 3. The braces are basically the same.
Lambda omitted format
Omit rules:
Parameter Type 1. Inside parentheses may be omitted;
2. If the parentheses and only one parameter, the parentheses may be omitted;
3. If there is one and only one statement, regardless of whether there is a return value in curly braces, you can omit the braces, return keywords and semicolons
Lambda premise of use
Lambda syntax is very simple, there is no object-oriented complex constraints. But there are several issues that need special attention when using
1. Lambda must have an interface, and the interface and only requires an abstract method. Whether JDK built Runnable, Comparator interfaces or custom interfaces, only when there is an abstract interface methods and only when, can use Lambda.
2. Lambda must be inferred context. The method is a local variable or argument type must Lambda corresponding interface type to use as an example of the Lambda interface.
Note: There is only one interface and abstract method, referred to as "interface function."
Using lambda standard format (no reference no return)
Cook cook a given interface contains only abstract methods makeFood, no parameters and returns no value. as follows:
. 1 Package demosummary.lambda; 2 . 3 / ** . 4 * Cook cook a given interface contains only abstract methods makeFood, no parameters and returns no value. As follows: . 5 * / . 6 public interface Cook { . 7 void makeFood (); . 8 }
In the following code, use the standard format for call invokeCook Lambda method, the printout "eat it!" The words:
. 1 Package demosummary.lambda; 2 . 3 / ** . 4 * In the following code, use the standard format Lambda call invokeCook method, printout words: "eat it!" . 5 * / . 6 public class InvokeCook { . 7 public static void main (String [] args) { . 8 // invokeCook (() -> { . 9 // System.out.println ( "eat"); 10 // }); . 11 12 is // omitted format 13 is invokeCook (() -> System.out.println ( "eat" )); 14 } 15 16 private static void invokeCook(Cook cook) { 17 cook.makeFood(); 18 } 19 }
Note: Parentheses parameters Cook Interface makeFood abstract approach represents is empty, the method braces on behalf of makeFood body.
Using a Lambda standard format (with reference return a)
Given a calculator Calculator interface calc containing abstract methods may be obtained by adding the two numbers and int values:
1 package demosummary.lambda; 2 3 public interface Calculator { 4 int cal(int a, int b); 5 }
In the following code, using the standard format invokeCalc Lambda call method, is calculated by adding the completion of 120 and 130:
1 package demosummary.lambda; 2 3 public class invokeCal { 4 public static void main(String[] args) { 5 // invokeCal(120,130,(int a , int b) -> { 6 // return a + b; 7 // }); 8 9 //省略格式 10 invokeCal(120,130,(int a , int b) -> a + b); 11 } 12 13 public static void invokeCal(int a , int b , Calculator calculator){ 14 int result = calculator.cal(a, b); 15 System.out.println("输出结果:"+result); 16 } 17 }
Lambda parameters and return values
Requirements: 1. Person object using a plurality of memory array 2. Arrays sort method of Person object array in ascending order by Age
1 package demosummary.lambda; 2 3 public class Person { 4 private String name; 5 private int age; 6 7 public Person() { 8 } 9 10 public Person(String name, int age) { 11 this.name = name; 12 this.age = age; 13 } 14 15 public String getName() { 16 return name; 17 } 18 19 public void setName(String name) { 20 this.name = name; 21 } 22 23 public int getAge() { 24 return age; 25 } 26 27 public void setAge(int age) { 28 this.age = age; 29 } 30 31 @Override 32 public String toString() { 33 return "Person{" + 34 "name='" + name + '\'' + 35 ", age=" + age + 36 '}'; 37 } 38 }
. 1 Package demosummary.lambda; 2 . 3 Import java.util.Arrays; . 4 . 5 public class LambdaComparator { . 6 public static void main (String [] args) { . 7 the Person [] Array = { . 8 new new the Person ( "Gülnezer Bextiyar" , 22 is ), . 9 new new the Person ( "Dilly Reba", 21 is ), 10 new new the Person ( "Ouyang Nana",. 19 ) . 11 }; 12 is 13 is // Arrays.sort (Array, (the Person A, the Person B) -> { 14 // return a.getAge() - b.getAge(); 15 // }); 16 17 //省略格式 18 Arrays.sort(array,(Person a, Person b) -> a.getAge() - b.getAge()); 19 20 for (Person person : array) { 21 System.out.println(person); 22 } 23 } 24 }