1 General description
JAVA8 new features Overview
Function-Type interface
Lambda expression (closure)
2 JAVA8 new features overview
Oracle Corporation released the JAVA8 version in March 2014, the most revolutionary version since JDK5.0.
Java8 brings a number of new features to the Java language, compilers, class libraries, and JVMs. The next section will detail the new features of JAVA8 in the Java language and their usage scenarios.
3 Function-type interface
A core concept introduced by JAVA8 is the functional interface (functional Interfaces): If an interface defines a unique abstract method, then this interface is a functional interface. It also introduces a new annotation: @FunctionalInterface, using the annotation above a functional interface to declare this interface to be a functional interface means that the interface can only have a unique abstract method.
Threads are usually created with the implementation of the Runnable interface, and then the abstract run method is overridden to define the thread task. The interface is a function-type interface.
//函数式接口注解,此接口中不能添加其他抽象方法publicinterface Runnable { /*该方法由子类实现,定义线程执行的业务逻辑*/ publicabstractvoidrun();}
So the question is, what's the use of functional interfaces? Take a look at the lambda expression below
4 lambda expression (closure)
Important properties of functional interfaces: You can instantiate them using lambda.
The introduction of Lambda has a number of benefits for developers: Before JAVA8, anonymous internal classes, listeners and event handlers used to be lengthy, code readability was poor, and lambda expression applications made the code more compact and readability enhanced. (The precondition is that you understand the meaning of the lambda expression) and the lambda expression makes it convenient to have large collections of parallel operations, giving full play to the advantages of multicore CPUs and making it easier to write code for multi-core processors;
4.1 The composition of lambda expressions
The first part: a comma-delimited form parameter in parentheses, which is a parameter in a function-type interface method.
Part II: For an arrow symbol:->
The third part: The method body, can be an expression or a code block, if the method body is an expression, the value of the expression is returned as a return value, if the method body is a code block, need to use {} wrapped up, need a return value, but if the function interface method return value is void, there is no need to return a value.
Lambda representation (parmeters), expression or (parmeters), {statements;}
4.2 Best Practices for lambda expressions: replacing anonymous inner classes
The following example uses the Runnable interface to compare the notation of the lambda interface and the anonymous inner class
Use ()->{} instead of anonymous inner class
The Lambdasample class contains two methods, namely the use of a lambda expression and an anonymous inner class to calculate the even number between 1-10, and a more concise lambda expression relative to an anonymous inner class.
PackageTony.javacore.jdk8.lambda;Import Statictony.javacore.util.io.printutils.*;ImportJava.util.concurrent.Executor;ImportJava.util.concurrent.TimeUnit;/** * <P>LAMBDA expression Usage and anonymous internal class comparison </p> * @author Tony [email protected] * @project_name JavaCore * @package_name tony.javacore.jdk8.lambda * @file_name Lambdasamplevsanonymous.java * @ See * @version September 6, 2015 PM 9:36:49 * @since jdk8.0u45 * * Public class lambdasamplevsanonymous{ /** * Use lambda expressions to calculate even numbers between 1-10 and * @version September 6, 2015 PM 9:58:11 * @since * * Public Static void Getsumwithlambda() {Runnable runnable= ()->{println ("Creates an Runnable object with a lambda expression and calculates even numbers between 1-10 and");intsum =0; for(intI=1; i<=Ten; i++) {if(i%2==0) {sum+=i; }} println ("An even number between 1-10 and is:"+sum); }; Thread thread =NewThread (runnable); Thread.Start ();//Pause for 2 seconds after calculation is complete Try{TimeUnit.SECONDS.sleep (2); }Catch(Interruptedexception e) {E.printstacktrace (); } }/** * Use anonymous inner classes to calculate even numbers between 1-10 and * @version September 6, 2015 PM 10:00:31 * @since * * Public Static void getsumwithanonymous() {Runnable Runnable =NewRunnable () {@Override Public void Run() {println ("Creates an Runnable object with an anonymous inner class and calculates even numbers between 1-10 and");intsum =0; for(intI=1; i<=Ten; i++) {if(i%2==0) {sum+=i; }} println ("An even number between 1-10 and is:"+sum); } }; Thread thread =NewThread (runnable); Thread.Start (); } Public Static void Main(string[] args) {Getsumwithlambda (); Getsumwithanonymous (); }}/** * Create an Runnable object with a lambda expression and calculate even numbers between the even and * 1-10 between 1-10 and as: 30 * Create an Runnable object with an anonymous inner class and calculate an even number between 1-10 and * 1-10 and a: 30 * /
4.3 Lambda expression Best practices: traversing common data structures
PackageTony.javacore.jdk8.lambda;Import Statictony.javacore.util.io.printutils.*;ImportJava.util.ArrayList;ImportJava.util.Collection;ImportJava.util.HashMap;ImportJava.util.List;ImportJava.util.Map;ImportJava.util.Set;/** * <p> two ways to iterate through the collection </p> * @author Tony [email protected] * @project_name JavaCore * @ package _name Tony.javacore.jdk8.lambda * @file_name Foreachvslambda.java * @see * @version /c6> September 10, 2015 Morning 10:14:10 * @since jdk8.0u45 * * Public class foreachvslambda { /** * JDK1.5 's foreach traversal data structure list way * @param list */ Public Static void foreachcollection(collection<string> Collection) { for(String content:collection) {println ("Content:"+content); } }/** * New default method after using JDK1.8 foreach and lambda traversal data structure list * @param list */ Public Static void lambdaforeachcollection(collection<string> Collection) {Collection.foreach (content)->{println ("Content:"+content); }); }/** * foreach Loop traversal map * @param Map */ Public Static void Foreachmap(map<string, integer> Map) { for(map.entry<string,integer> Entry:map.entrySet ()) {println ("key:"+entry.getkey () +"Value:"+entry.getvalue ()); } }/** * New default method after using JDK1.8 foreach and lambda traversal data structure map * @param map * / Public Static void Lambdaforeachmap(map<string, integer> Map) {Map.foreach (Key,value)->{println ("key:"+key +"Value:"+value); }); } Public Static void Main(string[] args) {List<string> List =NewArraylist<> (); List.add ("Java"); List.add ("Scala"); List.add ("Jpython"); Foreachcollection (list); Printflag (); Lambdaforeachcollection (list); map<string, integer> map =NewHashmap<string, integer> (); Map.put ("Chinese", the); Map.put ("Math", the); Map.put ("English", the); Foreachmap (map); Printflag (); Lambdaforeachmap (map); }}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Java8 new features of Java Core technology-LAMBDA expressions