Code Pumpkin

Lambda Expression | Java8

April 21, 2017
Posted by Dipen Adroja

In this article, we will go through the lambda expression which is introduced as  a part of Java8. This is one of my favorite feature from the Java8 apart from the Stream. This feature we can see as Java's support to the functional programming language. In simple word we can treat lambda expression as an alternative of anonymous class implementation of functional interfaces.

Lambda expressions simplifies development a lot, makes code more readable and more maintainable as the code is going to be more declarative rather than implementive. When we use lambda expression along with the newly introduced java stream collections it makes out code more declarative if done in proper way. The basic syntax of the lambda expression as below:

(Comma separated Arguments) -> expression body

The following are the valid syntax for the lambda expressions.
Lambda Expression

Here, when there is a single parameter as a argument for the lambda expression we can avoid the parentheses. In case of zero or more than two arguments the parentheses are compulsory. If our lambda expressions contain more than one line of code then it is compulsory to have curly braces. In normal cases it is not advisable to have more line of code in lambda expression as it will loose the readability of the code. In such cases we can always create other function and call it from the expression body.

The simple example of the lambda expression is shown below. The code snippet is for adding two variable values and assign the value.

List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
//This will iterate through all the elements in the collection
//and print all the numbers from the collection.
numbers.forEach(t -> System.out.println(t));

Lambda is used to implement the functional interfaces as well. The advantage of using lambda expression instead of anonymous class for functional interface is lambda does not create separate class as it get created in case of anonymous class. The compiler create invoke dynamics for the lambda functions which will reduce the memory footprint created in the jar by the anonymous inner classes thus improving the application performance.

In below example, I have demonstrated creating a thread object by anonymous inner class as well as using the lambda.

package com.codepumpkin;

public class Sample {

	public static void main(String[] args){
	//Here to just write a line we wrapped the line into a function and again wrapped it into an anonymous class.
		Thread th= new Thread(
				new Runnable() {
			public void run() {
				System.out.println("Running in new thread");
        //Using lambda we can achieve the same using less code and more readability.	
		th= new Thread(() -> System.out.println("Running in new thread"));
		System.out.println("Main thread running");

Here, one question should arise that how the compiler is knowing that this lambda is for the implementation for the run method of the runnable. When there is a functional interface(Interface which is having only one method is called functional interface),  lambda function will automatically detects and implements the functional interface as anonymous function. Here the mandatory condition is that the function should match with the number of argument in the functional interface.

You can if you want understand it in more detail you can also watch the below video.

That's all for this topic. If you guys have any suggestions or queries, feel free to drop a comment. We would be happy to add that in our post. You can also contribute your articles by creating contributor account here.

Happy Learning 🙂

If you like the content on CodePumpkin and if you wish to do something for the community and the planet Earth, you can donate to our campaign for planting more trees at CodePumpkin Cauvery Calling Campaign.

We may not get time to plant a tree, but we can definitely donate ₹42 per Tree.

About the Author

Coder, Blogger, Wanderer, Philosopher, Curious pumpkin

Tags: , ,

Comments and Queries

If you want someone to read your code, please put the code inside <pre><code> and </code></pre> tags. For example:
<pre><code class="java"> 
String foo = "bar";
For more information on supported HTML tags in disqus comment, click here.
Total Posts : 124
follow us in feedly

Like Us On Facebook