In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces you how to understand lambda expression, the content is very detailed, interested friends can refer to, hope to be helpful to you.
What is functional programming?
Before we learn about lambda, let's figure out what a function is. The function I understand is to input something and output it after a certain rule. If we buy an apple over time and the unit price of the apple is 5 yuan, then f (x) = 5x; where x represents the weight of the apple, then a relationship between the weight and the price of the apple is a function. Let's take a look at this function, and we're really focused on x and 5x. In this way, we can easily understand that the focus of the function is on the input parameters and the corresponding function rules, and nothing else is important. Let's analyze it with a piece of java code:
Functional interface @ FunctionalInterfacepublic interface Apple {int buyApple (int x);} method implementation
We rewrite this implementation based on the previously defined function combined with Lambda-- "focus on input parameters and function rules", that is, the only code we focus on is:
(int x) {return 5roomx;}
Then we add the lambda operator to get the following code
Apple apple = (int x)-> {return 5roomx;}; functional interface
Our lambda expression is based on a functional interface, and "lambda specifies that there can only be one method in the interface that needs to be implemented." That is, there can be multiple methods, but only one method needs to be implemented. Methods decorated by default in the java8 interface will have a default implementation.
The @ FunctionalInterface annotation is used to modify a functional interface, which requires only one method that has not been implemented.
Simplification of lambda expression
The syntax form of lambda is ()-> {}, where () is used to describe the parameter list, {} is used to describe the method body, and-> is the lambda operator, pronounced (goes to).
Simplify parameter types, you can not write parameter types, but you must not write parameter types for all parameters. Apple apple = (x)-> {return 5 parentheses;}; simplify parentheses. If there is only one parameter, you can omit parentheses. Apple apple = x-> {return 5roomx;}; simplify the curly braces of the method. If the method has only one statement, you can omit the curly braces, and if there is a return value, you cannot write return. Apple apple = x-> 5x
We don't need to use traditional optimization like this, and when we're done, our overall code looks like this.
@ FunctionalInterfacepublic interface Apple {int buyApple (int x);} public static void main (String [] args) {Apple apple = x-> 5roomx;} on how to understand lambda expressions is shared here, I hope the above content can be helpful to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.
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.