Example 1, using lambda expression to implement runnable
When I started using Java 8 o'clock, the first thing I did was to replace the anonymous class with a lambda expression, and the implementation of the Runnable interface was the best example of an anonymous class. Take a look at the runnable implementation before Java 8, which requires 4 lines of code, whereas using a lambda expression requires only one line of code. What have we done here? That is to replace the entire anonymous class with the () {} code block.
1 // Before Java 8: 2 new Thread (new Runnable () { 3 Span style= "COLOR: #000000" > @Override 4 pu Blic void run () { 5 System.out.println (" Before Java8, too much code for too little-do " ); 6 } 7 }). Start ();
1 // Java 8 Way: 2 New Thread (()->system.out.println ("in Java8, Lambda expression Rocks!"). Start ();
Output:
1 for Do 2 LAMBDA expression Rocks!!
This example shows us the syntax of the Java 8 lambda expression. You can use lambda to write the following code:
1 (params), expression2 (params), statement3
For example, if your method does not modify and rewrite the parameters, just print something in the console, then you can write:
1 ()-System.out.println ("Hello Lambda Expressions");
If your method receives two parameters, it can be written as follows:
(intint odd), even + odd
Incidentally, the name of the internal variable of the lambda expression is usually shortened. This allows the code to be shortened and put on the same line. Therefore, in the above code, the variable name to choose a, b or X, Y will be better than even, odd.
Example 2, using a Java 8 lambda expression for event handling
If you have used the Swing API programming, you will remember how to write the event listener code. This is another classic use case for an old version of a simple anonymous class, but now it's not possible. You can use lambda expressions to write better event-listening code, as follows:
1 // Before Java 8: 2 JButton show = new JButton ("Show" 3 show.addactionlistener (new ActionListener () { 4 @ Override 5 public void actionperformed (ActionEvent e) { 6 System.out.println (" Event handling without lambda expression is boring " ); 7 } 8 });
1 // Java 8 Way: 2 Show.addactionlistener (e), {3 System.out.println ("Light, Camera, Action!! Lambda Expressions Rocks "); 4 });
Another place where Java developers often use anonymous classes is to customize the Comparator for Collections.sort (). In Java 8, you can replace an ugly anonymous class with a more readable lambda expression. It should not be difficult for me to leave this for practice, as I do in the process of implementing Runnable and ActionListener using lambda expressions.
Example 3, iterating the list using a lambda expression
If you have been in Java for a few years, you know that the most common operation for a collection class is to iterate and apply the business logic to individual elements, such as a list of orders, transactions, and events. Since Java is an imperative language, all of the loop code before Java 8 is sequential, meaning that its elements can be parallelized. If you want to do parallel filtering, you need to write your own code, which is not so easy. By introducing lambda expressions and default methods, the question of what to do and how to do it is separated, which means that the Java collection now knows how to iterate, and the collection elements can be processed in parallel at the API level. In the following example, I'll show you how to iterate through a list using lambda or not using a lambda expression. You can see that the list now has a ForEach () method that iterates over all the objects and applies your lambda code to it.
1 // before Java 8: 2 List features = Arrays.aslist ("Lambdas", "Default Method", "Stream API", "Date and Time API"); 3 for (String feature:features) {4 System.out.println (feature); 5 }
1 // after Java 8: 2 List features = Arrays.aslist ("Lambdas", "Default Method", "Stream API", "Date and Time API"); 3 Features.foreach (n- System.out.println (n)); 4 5 // a method reference using Java 8 is more convenient, and the method reference is indicated by:: Double-colon operator, 6 // scope resolution operators that look like C + + 7 Features.foreach (system.out::p rintln);
Output:
1 Lambdas 2 Default Method 3 Stream API 4 Date and Time API
The last example of a list loop shows how to use method reference in Java 8. You can see that the double colon, scope resolution operators in C + + are now used in Java 8 to represent method references.
This article originates from: http://www.importnew.com/16436.html
Java 8 lambda Expression Example