The article represents different viewpoints on Java Lambda Expressions (Java 8) to help Java developers understand what, why, when, how of Lambda expressions.
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
System.out.println("button clicked");
}
});
The above can be avoided by use of what is termed as Lambda Expressions. Lambda expression can be used to easily associate behavior of printing “button clicked” without having need of boilerplate code in following manner:
Note that boilerplate code is no more needed. The code also becomes easy to read and understand. The above code represents that the fact that one argument “event” is passed to the code inside the braces.
Lambda Expression with No Arguments
This is used to implement Runnable interface which has got just one method such as run() that takes no argument and return void type.
OR,
Lambda Expression with Multiple Arguments
Below represents examples when there are multiple arguments for methods and which need to be represented as Lambda expression:
Above represents Lambda expression assigned to “add” variable which is equivalent to method with two argument x, and y and statement that adds up x and y. Note that “add” variable does not store the value of addition of x and y. However, it represents the code that adds up the variable.
[adsenseyu1]
Last updated: 15th May, 2024 Have you ever wondered how your bank decides what to…
In this fast-changing world, the ability to learn effectively is more valuable than ever. Whether…
Last updated: 13th May, 2024 Whether you are a researcher, data analyst, or data scientist,…
Last updated: 12th May, 2024 Data lakehouses are a relatively new concept in the data…
Last updated: 12th May 2024 In this blog, we get an overview of the machine…
Last updated: 12th May, 2024 In the world of generative AI models, autoencoders (AE) and…