In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly talks about "the usage of Lambda expression of Java8". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "the use of Lambda expressions for Java8".
1. Java 8 Lambda expression
Lambda expressions (closures), which are the most important new features that drive the release of Java 8. A function is allowed as an argument to a method (the function is passed into the method as an argument).
1.1 Grammar
(parameters)-> expression or (parameters)-> {statements;}
The following are important features of lambda expressions:
Optional type declaration: there is no need to declare parameter types, the compiler can uniformly identify parameter values.
Optional parameter parentheses: one parameter does not need to define parentheses, but multiple parameters need to define parentheses.
Optional curly braces: if the body contains a statement, you do not need to use braces.
Optional return keyword: if the body has only one expression return value, the compiler will automatically return a value, and curly braces need to specify that the expression returns a value.
1.2lambda expression instance package com.lambda;public class Java8Tester {public static void main (String args []) {Java8Tester tester = new Java8Tester (); / / Type declaration MathOperation addition = (int a, int b)-> a + b; / / No type declaration MathOperation subtraction = (a, b)-> a-b / / return statement MathOperation multiplication = (int a, int b)-> {return a * b;}; / / No curly braces and return statement MathOperation division = (int a, int b)-> a / b; System.out.println ("10 + 5 =" + tester.operate (10,5, addition)) System.out.println ("10-5 =" + tester.operate (10,5, subtraction)); System.out.println ("10 x 5 =" + tester.operate (10,5, multiplication)); System.out.println ("10 / 5 =" + tester.operate (10,5, division)); / / No parentheses GreetingService greetService1 = message-> System.out.println ("Hello" + message) / in parentheses GreetingService greetService2 = (message)-> System.out.println ("Hello" + message); greetService1.sayMessage ("Runoob"); greetService2.sayMessage ("Google");} interface MathOperation {int operation (int a, int b);} interface GreetingService {void sayMessage (String message) } private int operate (int a, int b, MathOperation mathOperation) {return mathOperation.operation (a, b);}}
Output result:
There are two things to note when using Lambda expressions:
Lambda expressions are mainly used to define a method type interface for inline execution, for example, a simple method interface. In the above example, we use various types of Lambda expressions to define the methods of the MathOperation interface. Then we define the execution of sayMessage.
Lambda expressions eliminate the hassle of using anonymous methods and give Java simple but powerful functional programming capabilities.
1.3 lambda variable scope
1.3.1 lambda expressions can only refer to outer local variables marked with final, which means that local variables defined outside the domain cannot be modified within lambda, otherwise an error will be compiled.
1.3.2 access to outer local variables in lambda expressions
1.3.3 the local variable of the lambda expression may not be declared as final, but it must not be modified by the following code (that is, implicitly has the semantics of final), and the lamdba variable cannot be repeated with the local variable.
At this point, I believe you have a deeper understanding of "the use of Lambda expressions in Java8". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.