Core Java

Java 8 – Why use Lambda Expressions?

Introduction :

Java 8 introduced the concept of Lambda expressions which enables you to implement functional-style programming in Java. Functional programming allows you to focus on ‘what to do’, rather than ‘how to do it’. It allows you to pass in code blocks as a method argument, instead of simply passing the data.

This can be very interesting and of high use for us as programmers. To understand it , consider a situation when you are writing a code of filtering out some contents from a given data structure(say an ArrayList) based on different conditions. How will you go ahead implement it using Java 7 or other lower Java versions ?

Filtering using Customised Methods:

If you are a newbie, you’ll implement something like :

Filtering using the concept of Interfaces :

If you are an experienced Java Programmer with the knowledge of interfaces, you’ll refine your code like this:

But still , if you see even in the above approach, their is duplication of code in terms of the fact that you’ll have to implement new classes each time you go ahead writing a new type of filter. All time favourite question of all programmers – Can we do better? Till Java 7, No.

Yes for Java 8, using Lambda expressions. We’ll look at it in a while.

Functional Interface :

Functional Interface is nothing more than any Java interface which has exactly one non-Object class abstract method. It can have any number of default and static methods.

So, if you now understand the definition of Functional Interfaces, you’ll guess it right that Runnable, Comparable etc are also functional interfaces. Apart from the pre-existing functional interfaces, there are many new functional interfaces defined in Java 8 in java.util.function package like Predicate<T>, Function<T,R>, Supplier<T>, Consumer<T> etc. You can learn about each one of them at the Oracle website here.

Lambda Expression :

Lambda expressions implement functional interfaces by defining anonymous functions which can be passed as a method argument or used as values in a program.

Filtering with the use of Lambda Expressions & Predicate<T> :

Now, let’s move back to the original problem of filtering out the contents and let’s try to implement the same logic using the concept of Functional Interface and Lambda expressions, it simplifies to the one given below:

In the above code, we have used a predefined Functional Interface  РPredicate<T> of package java.util.function which has the following definition:

The code that will be executed when test() method is invoked is determined by the lambda expression to which this Functional Interface refers at any given time.

The code has become so clean and elegant. Also, if we intend to add more such filters , it is a matter of just adding a few more predicates(one-liners) and we are sorted.

Isn’t it beautiful?

Conclusion :

We have seen only one scenario of why use Lambda Expressions. There are a lot of predefined functional interfaces in java.util.function package like Consumer<T>, Supplier<T>, Function<T,R>, Predicate<T> etc that serve different purposes when used in our code.

Also, the collection framework has exposed many new methods to support and encourage the use of lambda expressions. Some simple example :

To conclude, the use of Lambda expressions makes our life a lot easier as a developer and beautifies our code, reducing LoC(Lines of code) significantly.


Be the First to comment.

Leave a Comment

Your email address will not be published. Required fields are marked *