Java core technology-New Features of Java 8-Lambda expressions
1. General description
Java 8 new features
Functional Interface
Lambda expressions (closures)
2 Java 8 new features
Oracle released the official Java 8 version in March 2014, which is the most revolutionary version since JDK5.0.
Java 8 brings a large number of new features to the Java language, compiler, class library, and JVM. The following content will detail the new features of Java 8 in terms of the Java language and their application scenarios.
3. functional interfaces
One of the core concepts introduced by Java 8 is Functional Interfaces (Functional Interfaces): If an interface defines a unique abstract method, this interface is a Functional interface. At the same time, a new annotation is introduced: @ FunctionalInterface. Use this annotation above the functional interface to declare this interface as a functional interface, which means that this interface can only have a unique abstract method.
When a thread is created, the Runnable interface is usually used, and the abstract run method is rewritten to define a thread task. This interface is a functional interface.
@ FunctionalInterface // function interface annotation. Other Abstract METHODS public interface Runnable cannot be added to this interface {/* This method is implemented by subclass, define the business logic of thread execution */public abstract void run ();}
So the question is, what is the use of functional interfaces? See the following Lambda expressions.
4. Lambda expressions (closures)
Important Properties of functional interfaces: they can be instantiated using Lambda.
The introduction of Lambda has brought a lot of benefits to developers: Before Java 8, the use of anonymous internal classes, listeners and event processors was lengthy and the code was poorly readable, the Application of Lambda expressions makes the code more compact and more readable. (The premise is that you need to understand the meaning of Lambda expressions) and Lambda expressions make it easy to operate a large set of parallel operations and give full play to the advantages of multi-core CPUs, it is easier to write code for multi-core processors;
4.1 composition of Lambda expressions
Part 1: a parameter separated by commas (,) in brackets. A parameter is a parameter in a function interface method.
The second part is an arrow Symbol:->
Part 3: Method body, which can be an expression or code block. If the method body is an expression, the value of this expression is returned as the return value. If the method body is a code block, use {} to enclose it, A return value is required. However, if the return value of a function interface method is void, no return value is required.
Lambda is expressed as (parmeters)-> expression or (parmeters)-> {statements ;}
4.2 Best Practices for Lambda expressions: replacing anonymous internal classes
The following uses the Runnable interface as an example to compare the Lambda interface and anonymous internal class writing method.
Replace the anonymous internal class with ()-> {}
The LambdaSample class contains two methods: using a lambda expression and an anonymous internal class to calculate an even number between 1 and 10. Lambda expressions are more concise than anonymous internal classes.
package tony.javacore.jdk8.lambda;import static tony.javacore.util.io.PrintUtils.*;import java.util.concurrent.Executor;import java.util.concurrent.TimeUnit;/** *
Comparison between Lambda expressions and anonymous internal classes
* @ Author tony 18616939520@163.com * @ project_name JavaCore * @ package_name tony. javacore. jdk8.lambda * @ file_name LambdaSampleVSAnonymous. java * @ see * @ version September 6, 2015 9:36:49 * @ since JDK8.0u45 */public class LambdaSampleVSAnonymous {/*** use lambda expressions to calculate the even numbers between 1 and 10 and * @ version September 6, 2015 9:58:11 * @ since */public static void getSumWithLambda () {Runnable runnable = ()-> {println (use a lambda expression to create a runnable object and calculate the even number and sum between 1 and 10); int sum = 0; for (int I = 1; I <= 10; I ++) {if (I % 2 = 0) {sum + = I ;}} println (an even number between 1 and 10 is: + sum) ;}; Thread thread = new Thread (runnable); thread. start (); // pause for 2 seconds after calculation. try {TimeUnit. SECONDS. sleep (2);} catch (InterruptedException e) {e. printStackTrace () ;}/ *** use an anonymous internal class to calculate an even number between 1 and 10 and * @ version 10:00:31 on January 1, September 6, 2015 * @ since */public static void getSumWithAnonymous () {Runnable runnable = new Runnable () {@ Override public void run () {println (create a runnable object using an anonymous internal class and calculate the even number and sum between 1 and 10 ); int sum = 0; for (int I = 1; I <= 10; I ++) {if (I % 2 = 0) {sum + = I ;}} println (the even number between 1 and 10 is: + sum) ;}; Thread thread = new Thread (runnable); thread. start () ;}public static void main (String [] args) {getSumWithLambda (); getSumWithAnonymous ();}} /*** use a lambda expression to create a runnable object and calculate the sum of the even numbers between 1-10 and * 1-10: 30 * Create a runnable object using an anonymous internal class and calculate the sum of the even numbers between 1-10 and * 1-10: 30 */
4.3 Lambda expressions best practices: traversing Common Data Structures
package tony.javacore.jdk8.lambda;import static tony.javacore.util.io.PrintUtils.*;import java.util.ArrayList;import java.util.Collection;import java.util.HashMap;import java.util.List;import java.util.Map;import java.util.Set;/** *
Two methods to traverse a set
* @ Author tony 18616939520@163.com * @ project_name JavaCore * @ package_name tony. javacore. jdk8.lambda * @ file_name foreachvslam.pdf. java * @ see * @ version September 10, 2015 10:14:10 * @ since JDK8.0u45 */public class ForeachVSLambda {/*** JDK1.5's foreach Method for traversing the data structure List * @ param list */public static void foreachCollection (Collection Collection) {for (String content: collection) {println (content: + content );}} /*** use the default method forEach added after JDK1.8 and lambda to traverse the data structure List * @ param list */public static void lambdaForEachCollection (Collection Collection) {collection. forEach (content)-> {println (content: + content) ;});}/*** foreach cyclically traverses map * @ param map */public static void foreachMap (Map Map) {for (Map. Entry Entry: map. entrySet () {println (key: + entry. getKey () + value: + entry. getValue () ;}}/*** use the new default method after JDK1.8 forEach and lambda to traverse the data structure Map * @ param map */public static void lambdaForEachMap (Map Map) {map. forEach (key, value)-> {println (key: + key + value: + value) ;});} public static void main (String [] args) {List List = new ArrayList <> (); list. add (Java); list. add (Scala); list. add (JPython); foreachCollection (list); printFlag (); lambdaForEachCollection (list); Map Map = new HashMap (); Map. put (language, 105); map. put (mathematics, 105); map. put (English, 105); foreachMap (map); printFlag (); lambdaForEachMap (map );}}