Lambda Expressions in Java

Nimesha Wijepala
3 min readNov 24, 2020

Introduction

Before java 8, software developers have to use anonymous classes or separate classes to implement interfaces with one abstract method which is called functional interfaces. Also, it had to write a bunch of code lines to iterate over java collections. But the headache continued, only until java 8 introduced the concept of lambda expressions. Let’s see how to use lambda expressions to make our code more efficient with less coding.

What is a Lambda Expression

A lambda expression is a block of code that can take arguments and return values. It works as a method but has no name and hence cannot be reused. The following describes the syntax of a lambda expression.

(X,Y) -> { block of code }

A lambda expression consists of 3 parts. We can pass zero or more arguments within parentheses like (X,Y) to the lambda expression. If it is only one argument, we can simply pass it without parentheses. The next part is the arrow sign. It links the passing arguments and the code block of the expression. We can write the logic and the return values inside the curly braces after the arrow sign. No need to add curly braces if there is only one line in the code block. Anyway, a lambda expression is created with these mentioned 3 parts.

How to Get Lambda Expressions in Action

Let’s see how to use lambda expressions in our implementations. As we discussed earlier, lambda expressions can be used to implement the abstract method in a functional interface. Runnable is one such interface that contains the run method and the following example describes the use of lambda expressions for the implementation.

output: Thread is calling via a lambda expression

Isn’t it easier than a separate overridden method or an anonymous class implementation? Of course, it is easy and reduces a lot of code lines.

Our next example describes how a lambda expression can be used to iterate over a hashmap.

output: Key: 1, Value: One
Key: 2, Value: Two
Key: 3, Value: Three

There are several ways to iterate over a hash map in java. But as you can see using a lambda expression is quite easy here.

Usually we use sort(List<T> list, Compartor<? super T> c) method in Collections class to sort an ArrayList of objects according to a specified attribute. Along with the sorting list, the method itself requires an parameter of implemented functional interface, Comparator. Using lambda expressions we can easily implement the abstract method, int compare(T o1, T o2) inside the Comparator interface. Let’s go and see the example.

output: Marks: 80 *** Student Id: 3, Name: Jerry
Marks: 75 *** Student Id: 2, Name: Tom
Marks: 55 *** Student Id: 1, Name: Brian
Marks: 55 *** Student Id: 5, Name: Stuart
Marks: 45 *** Student Id: 4, Name: John

Here we have sorted students in descending order based on their marks. There are only 2 lines needed for both sorting and iterating over the list. As you can clearly see, the code has become more efficient with lambda expressions.

Conclusion

Lambda expression is the way of implementing functional interface method which is introduced by java 8. Also it reduces the lines of code to make the coding more efficient and easier. So guys, let’s try lambda expressions with java.

--

--

Nimesha Wijepala

One who work in software industry as well as loves writing. Trying to combine both for sharing knowledge all over the world.