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/02 Report--
This article mainly introduces "what is the Lambda expression in Java". In the daily operation, I believe that many people have doubts about what the Lambda expression in Java is. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubt about "what is the Lambda expression in Java?" Next, please follow the editor to study!
The following is a basic example of syntax:
(parameter1, parameter2) = > expression
Or
(parameter1, parameter2) = > {code block}
The Lambda expression is very limited, and if it is not void, you must return a value immediately. They cannot use keywords such as if or for to keep it simple. If you need more lines of code, you can use code blocks instead.
Now you can't just use expressions when implementing lambda expressions. Lambda is the implementation of a functional interface. A functional interface is an interface with only one abstract method. The advantage of lambda is that they allow you to implement methods without the need for classes and instantiated objects that implement interfaces.
Here is an example:
Interface FuncInterface {/ / abstract function void abstractFun (int x); / / non-abstract (or default) function default void normalFun () {System.out.println ("Hello");}} class Test {public static void main (String args []) {/ / implements the lambda expression of the above functional interface. / / this interface implements abstractFun () FuncInterface fobj = (int x)-> System.out.println (2roomx) by default; / / this invokes the above lambda expression and prints 10. Fobj.abstractFun (5);}}
Lambda expressions are often used as arguments to functions. To improve readability, you can also store lambda expressions in variables, as long as the type is an interface with only one method, the same number of parameters, and the same return type.
Import java.util.ArrayList;import java.util.function.Consumer;public class Main {public static void main (String [] args) {ArrayList numbers = new ArrayList (); numbers.add (5); numbers.add (9); numbers.add (8); numbers.add (1); Consumer method = (n)-> {System.out.println (n);}; numbers.forEach (method);}}
A common use of lambda is to create threads. This is an example of using a lambda code block to implement a Runnable object for thread execution.
/ / runnable LambdaRunnable task2 = ()-> {System.out.println ("Task # 2 is running");}; / / start the thread new Thread (task2) .start (); at this point, the study of "what is the Lambda expression in Java" is over, hoping to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.