In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "lambda expression and traditional interface function how to achieve", the content of the article is simple and clear, easy to learn and understand, now please follow the editor's train of thought slowly in depth, together to study and learn "lambda expression and traditional interface function how to achieve" it!
I. Interface definition
First of all, we need to understand what the lambda expression is saying. The answer is that lambda expressions express the implementation of interface functions, so we need to do some preparatory work. In the traditional way of development, we are not used to passing code blocks to functions. All of our behavior definition code is encapsulated in the method body and executed through an object reference, just like using the following code:
Public class LambdaDemo {/ / function definition public void printSomething (String something) {System.out.println (something);} / / call function public static void main (String [] args) {LambdaDemo demo = new LambdaDemo (); String something = "I am learning Lambda"; demo.printSomething (something);}} by creating an object
You should be familiar with the way the above code is developed, which is the implementation style of the classic OOP. Let's make a change to the above code to create a functional interface and define abstract methods for that interface.
Public class LambdaDemo {/ / Abstract function interface interface Printer {void print (String val);} / pass function interface public void printSomething (String something, Printer printer) {printer.print (something);}} via parameter
Second, the traditional implementation of interface functions.
In the above implementation, the Printer interface is responsible for printing behavior, which can be console printing or other printing behavior. The method printSomething no longer defines the behavior, but instead executes the behavior defined by Printer, which makes the design more flexible. The code is as follows:
Public static void main (String [] args) {LambdaDemo demo = new LambdaDemo (); String something = "I am using a Functional interface"; / / implement Printer interface Printer printer = new Printer () {@ Override public void print (String val) {/ / console print System.out.println (val);}}; demo.printSomething (something, printer);}
None of us have used lambda expressions so far. We only created a concrete implementation of the Printer interface and passed it to the printSomething method.
Third, the implementation of lambda expression.
Later on the concept of lambda expressions, let's first learn the syntax of lambda expressions:
(param1,param2,param3..., paramN)-> {/ / Code Block;}
First of all, we know the lambda expression, which expresses the function's comma-separated formal parameter list on the left side of the interface function arrow and the function body code on the right.
Now, let's ReFactor the code in the first section using the lambda expression
Public static void main (String [] args) {LambdaDemo demo = new LambdaDemo (); String something = "I am learning Lambda"; / / implement the Printer interface (follow this line of lambda expression code) Printer printer = (String toPrint)-> {System.out.println (toPrint);}; / / call the interface to print demo.printSomething (something, printer);}
Lambda expressions make our code more concise. In fact, using lambda expressions has more benefits in performance and multicore processing, but they only make sense after you understand java8 Streams API, so they are outside the scope of this article (as described in previous articles).
Compared with the implementation of traditional java code, is the amount of code reduced a lot? But this is still not the simplest way to achieve, let's take it one step at a time.
Printer printer = (String toPrint)-> {System.out.println (toPrint);}; / / simplify: remove parameter type Printer printer = (toPrint)-> {System.out.println (toPrint);}; / / simplify: remove parameter brackets Printer printer = toPrint- > {System.out.println (toPrint);}; / / simplify: remove function body curly brackets Printer printer = toPrint- > System.out.println (toPrint)
Even if the type of the parameter is not specified on the left side of the arrow, the compiler will infer the type from the formal parameter of the interface method. When there is only one parameter, we can omit the parenthesis of the parameter. When the body of the function has only one line, we can omit the curly braces of the body of the function.
If our interface method definition does not take any parameters, we can replace it with empty parentheses:
()-> System.out.println ("anything you wan to print")
So, what kind of code do we end up simplifying with lambda expressions? The true face of Lushan Mountain:
Public static void main (String [] args) {LambdaDemo demo = new LambdaDemo (); String something= "I am Lambda"; / / follow the following line of code demo.printSomething (something, toPrint-> System.out.println (toPrint));}
We use the lambda expression to inline the function call parameters, reducing the original nine lines of the main method to only three lines. But the author wants to say that this is still not the ultimate way of code simplification that lambda expressions can accomplish. When you learn how to use java8 Stream API with lambda expressions, you will find that your coding efficiency will be greatly improved!
Thank you for reading, the above is the content of "how to achieve lambda expression and traditional interface function". After the study of this article, I believe you have a deeper understanding of how to achieve lambda expression and traditional interface function, and the specific use 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.