A quick introduction to lambdas in Java

Java lambdas

You’ve probably heard of lambdas before, and even seen their strange new syntax. But what is a lambda actually, and how can you use them?

Lambdas and functional programming have been around for many years in other computer programming languages.

What is a lambda?

The term lambda comes from the lambda calculus (λ-calculus), developed in the 1930s by Alonzo Church. Lambda calculus is the basis of all functional programming languages. The earliest functional programming language was LISP, developed in 1958. Another early language, Algol 60, had rules for procedures and variable binding that were closely related to those of lambda calculus.

In simple language, a lambda expression in Java is a short, concise in-line piece of code. We usually pass data (parameters) to code (methods). What we would really like to do is to create a short piece of code on the fly and pass it into a method.

The usual approach

In my previous post about Comparators, I used the following code:

// using an anonymous inner class to create 
// a Comparator object
TreeSet ts = new TreeSet<>(
               new Comparator<>() {
                 public int compare(
                    Employee e1, Employee e2) {
                      return e1.age - e2.age;
// adding lots of employee objects, sorting automatically
Employee e1 = new Employee();

In the line constructing the TreeSet, we passed a Comparator object, i.e. a piece of data. Wouldn’t be nice if we could have just passed the code directly to the constructor?

We can, using lambdas! (If we’re using Java 8 and above.)

The lambda approach

An intelligent compiler can work out that the TreeSet constructor needs a Comparator. It also knows that a Comparator implementation must override the compare() method. It can also infer from the parameterized TreeSet constructor that the Comparator must be parameterized with Employee as well.

The only things the compiler can’t work out are what we want to call the parameters to the compare() method, and what we want to do in the code block. This is where the lambda expression syntax comes into play:

A lambda expression represents a piece of code. The syntax is as follows:

( parameter_list ) -> { block of code }

The -> is pronounced “arrow”. To make it easier to understand, you can also say “results in” or “evaluates to”.

We could have replaced the anonymous inner class syntax with a lambda expression that represents the compare() method as follows:

// using an anonymous inner class to create a 
// Comparator object
TreeSet ts = new TreeSet<>(
   (Employee e1, Employee e2) -> { return e1.age - e2.age; }

The parameter types can be left out if the compiler can infer what they are. So the lambda expression could have been further simplified to:

TreeSet ts = new TreeSet<>(
               (e1, e2) -> { return e1.age - e2.age; }

If there’s only one line of code in the code block, the braces and even the return statement can be omitted:

TreeSet ts = new TreeSet<>(
                 (e1, e2) -> e1.age - e2.age

That’s it.

There! A quick and simple introduction to lambdas!

If you want more, and you haven’t seen it yet, watch my webinar on Lambdas.

And don’t forget to post a comment. Your comments help me write better tips that will help you.

Leave a Comment

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

Code like a Java Guru!

Thank You

We're Excited!

Thank you for completing the form. We're excited that you have chosen to contact us about training. We will process the information as soon as we can, and we will do our best to contact you within 1 working day. (Please note that our offices are closed over weekends and public holidays.)

Don't Worry

Our privacy policy ensures your data is safe: Incus Data does not sell or otherwise distribute email addresses. We will not divulge your personal information to anyone unless specifically authorised by you.

If you need any further information, please contact us on tel: (27) 12-666-2020 or email info@incusdata.com

How can we help you?

Let us contact you about your training requirements. Just fill in a few details, and we’ll get right back to you.

Your Java tip is on its way!

Check that incusdata.com is an approved sender, so that your Java tips don’t land up in the spam folder.

Our privacy policy means your data is safe. You can unsubscribe from these tips at any time.