In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "Lambda case Analysis of Java8". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "Lambda case Analysis of Java8".
1. Introduction to lambda expression
Lambda expression is one of the new features provided by Java8, which can also be called closure; it supports Java to do simple functional programming, that is to say, it can pass an anonymous function as a parameter of a method; its format is divided into three parts, the first part is the input parameter list, the second part is composed of-> fixed, and the third part is the method body.
Public class LambdaTest {public static void main (String [] args) {/ / create threads using lambda expressions Thread thread = new Thread (()-> {System.out.println ("thread running");}); thread.start ();}}
/ / Operation result
Thread running
2. Important features of lambda expressions
Optional parameter type declaration: there is no need to declare the type of the parameter, the compiler can uniformly identify the parameter value
Public class LambdaTest {private Integer a; public LambdaTest (Integer a) {this.a = a;} public void print (LambdaInterface lambdaInterface) {lambdaInterface.print (this.a);} public static void main (String [] args) {LambdaTest lambdaTest = new LambdaTest; / / declare parameter type System.out.println ("declare parameter type") LambdaTest.print ((Integer a)-> {System.out.println ("a:" + a);}); / / do not declare parameter type System.out.println ("do not declare parameter type"); lambdaTest.print ((a)-> {System.out.println ("a:" + a);}) }} interface LambdaInterface {void print (Integer a);}
/ / Operation result
Declare parameter types
A: 123
Do not declare parameter types
A: 123
Optional parameter parentheses: parentheses do not need to be defined for one parameter, but must be defined when there are no parameters or multiple parameters
Public class LambdaTest {private Integer a; public LambdaTest (Integer a) {this.a = a;} public void print (LambdaInterface lambdaInterface) {lambdaInterface.print (this.a);} public static void main (String [] args) {LambdaTest lambdaTest = new LambdaTest (123); / / define parameter parentheses System.out.println ("define parameter parentheses") LambdaTest.print ((a)-> {System.out.println ("a:" + a);}); / / one parameter may not define parentheses System.out.println ("one parameter may not define parameter parentheses"); lambdaTest.print (a-> {System.out.println ("a:" + a);}) }} interface LambdaInterface {void print (Integer a);}
/ / Operation result
Define parameter parentheses
A: 123
A parameter can not define parentheses
A: 123
Optional curly braces: if the method body has only one statement, you do not need to use braces
Public class LambdaTest {private Integer a; public LambdaTest (Integer a) {this.a = a;} public void print (LambdaInterface lambdaInterface) {lambdaInterface.print (this.a);} public static void main (String [] args) {LambdaTest lambdaTest = new LambdaTest (123); / / use method body curly braces System.out.println ("use method body braces") LambdaTest.print (a-> {System.out.println ("a:" + a);}); / / A statement may not use method curly braces System.out.println ("a statement may not use method curly braces"); lambdaTest.print (a-> System.out.println ("a:" + a));}} interface LambdaInterface {void print (Integer a) }
/ / Operation result
Use method body curly braces
A: 123
A statement does not use method body curly braces
A: 123
Optional return keyword: if the method body has only one expression return value statement, there is no need to declare the return keyword, but if there are curly braces, you must declare
Public class LambdaTest {private Integer a; private Integer b; public LambdaTest (Integer a, Integer b) {this.a = a; this.b = b;} public Integer sum (LambdaInterface lambdaInterface) {return lambdaInterface.calculate (this.a, this.b);} public static void main (String [] args) {LambdaTest lambdaTest = new LambdaTest (123,456) / / the declaration returns the keywords System.out.println ("declaration returns keywords"); Integer S1 = lambdaTest.sum ((Integer a, Integer b)-> {return a + b;}); System.out.println (S1) / / an expression return value statement may not declare the return keyword System.out.println ("an expression return value statement may not declare a return keyword"); Integer S2 = lambdaTest.sum ((a, b)-> a + b); System.out.println (S2);}} interface LambdaInterface {Integer calculate (Integer a, Integer b);}
/ / Operation result
Declaration returns keyword
five hundred and seventy nine
An expression return value statement can not declare a return keyword
five hundred and seventy nine
3. Restrictions of lambda expressions on extraterritorial variables
Lambda expressions have implicit final semantic restrictions on local variables outside the domain, but not on member variables.
Public class LambdaTest {private Integer a; private Integer b; public LambdaTest (Integer a, Integer b) {this.a = a; this.b = b;} public Integer sum (LambdaInterface lambdaInterface) {return lambdaInterface.calculate (this.a, this.b);} public static void main (String [] args) {LambdaTest lambdaTest = new LambdaTest (123,456); int c = 111l Integer S1 = lambdaTest.sum ((a, b)-> {/ / modifying extraterritorial local variables will result in compilation error c = 222; return a + b;}); System.out.println (S1); int d = 333 Integer S2 = lambdaTest.sum ((a, b)-> {/ / external modification of extraterritorial local variables used inside lambda expressions will also result in compilation errors return a + b + d;}); d = 444; System.out.println (S2);}} interface LambdaInterface {Integer calculate (Integer a, Integer b);} public class LambdaTest {private Integer a Private Integer b; private Integer c; public LambdaTest (Integer a, Integer b, Integer c) {this.a = a; this.b = b; this.c = c;} public Integer sum (LambdaInterface lambdaInterface) {return lambdaInterface.calculate (this.a, this.b);} public static void main (String [] args) {LambdaTest lambdaTest = new LambdaTest (123,456,789) Integer S1 = lambdaTest.sum ((a, b)-> {/ / No compilation error lambdaTest.c = 999; return a + b + lambdaTest.c;}); System.out.println (S1);}} interface LambdaInterface {Integer calculate (Integer a, Integer b);}
/ / Operation result
1578
4. Advantages and disadvantages of lambda expressions
Advantages:
1. Make the code more concise
two。 Reduce the creation of anonymous inner classes and save resources
Disadvantages:
1. Poor maintainability and must be familiar with the parameter list of abstract methods
two。 Poor readability, must have a certain depth of lambda expression
5. Usage scenarios of lambda expressions
Lambda expressions can be used when declaring a method whose formal parameter list contains one or more functional interfaces; for example:
Create a thread using the implementation Runnable interface
Create a FutureTask using the implementation Callable interface
Use four functional interfaces: consumer interface Consumer, provider interface Supplier, assertive interface Predicate, functional interface Function
6. The implementation principle of lambda expression
Lambda expressions are compiled into a corresponding static method for each lambda expression when the compiler compiles the java file through a specific syntax, thus proving that the lambda expression is not a syntax sugar.
/ / A view of the above LambdaTest.java compiled LambdaTest.class file using javap-p will result in the following result: javap-p LambdaTest.classCompiled from "LambdaTest.java" public class cn.jackiegu.java8.study.lambda.LambdaTest {private java.lang.Integer a; private java.lang.Integer b; private java.lang.Integer c; public cn.jackiegu.java8.study.lambda.LambdaTest (java.lang.Integer, java.lang.Integer, java.lang.Integer) Public java.lang.Integer sum (cn.jackiegu.java8.study.lambda.LambdaInterface); public static void main (java.lang.String []); private static java.lang.Integer lambda$main$0 (cn.jackiegu.java8.study.lambda.LambdaTest, java.lang.Integer, java.lang.Integer) } Thank you for your reading. The above is the content of "Lambda instance Analysis of Java8". After the study of this article, I believe you have a deeper understanding of the Lambda case Analysis of Java8, and the specific usage needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.