What is a lambda expression in Java? Where did the term 'lambda' come from?

Java lambda expressions explained

Due to various language constraints, lambda expressions had, until recently, never made it into the Java language. The concept had long been baked into other languages, such as Groovy and Ruby. That all changed with Java 8. As organizations slowly move to Java 8 and Java 11 platforms, more and more developers are getting the opportunity to use lambda expressions — or lambda functions, as they’re also called — in their code. This has generated a great deal of excitement but also confusion. Many developers have questions. So, why is this new language feature called a lambda function?

Why are they called ‘lambda functions?’

The term lamdba function actually has its roots in Calculus. In mathematics, a lambda function is one in which a single set of assigned variables are mapped to a calculation. Here are a few algebraic lambda functions. Anyone who took high school math should recognize them.

(x) = x2
(x, y) = x + y
(x, y, z) = x3 - y2 + z

For the first equation, if x is 3, the function would evaluate to 9. If x and y are both 2 for the second function, the result is 4. If x, y and z are 1, 2 and 3 in the third function, the calculated result is zero.

As you can see, a single set of variables are mapped onto a function, which generates a result. The corollary to computer science is to take a set of variables and map those variables to a single function. Let’s place extra emphasis on the word single. Lambdas work when there is only a single function to implement. The concept of a lambda completely falls apart in computer science when multiple methods get thrown into the mix.

The anonymous nature of lambda functions

A second point worth mentioning is that lambda functions are anonymous and unnamed. That’s not an obvious point when dealing with mathematic functions, but if you look at the first function listed earlier, the following was sufficient to explain everything that was going on:

(x) = x2

There was no need to give the function a name, such as:

basicParabola (x) = x2

In this sense, lambda functions are unnamed and anonymous.

Lambda functions in Java

This discussion on the etymology of lambda functions is interesting, but the real question is how these mathematical concepts translate into Java.

lambda function in Java

An example of a lambda function in a Java program.

In Java, there are many, many places in which a piece of code needs a single method implementation. And there are many interfaces in the Java API where only a single method needs to be implemented. Also known as functional interfaces, commonly used single-method interfaces include Flushable, Runnable, Callable, Comparator, ActionLIstener, FileFilter, XAConnection and RowSetWriter. Using any of these interfaces in Java can be somewhat cumbersome. For example, Comparator is a functional interface that allows you to rank objects for easy sorting. Code for sorting an array prior to Java 8 would look something like this:

Integer[] numbers = {5, 12, 11, 7};
Arrays.sort(numbers, new Comparator<Integer>() {
   public int compare(Integer a, Integer b) {
      return b - a;
   }             
});

System.out.println(Arrays.toString(numbers));

When you use a lambda function, the verbosity goes away, and the result is this:

Integer[] numbers = {5, 12, 11, 7};
Arrays.sort(numbers, (a, b) -> b-a);
System.out.println(Arrays.toString(numbers));
lambda expression code bloat

An implementation of the Comparator interface with and without a lambda function.

When you first learn to use lambda expressions, sometimes it’s easier to assign the lambda expression to the interface it’s implementing in a separate step. The prior example might read a bit clearer if coded with an additional line:

Integer[] numbers = {5, 12, 11, 7};
Comparator theComparator =  (a, b) -> b-a ;
Arrays.sort(numbers, theComparator); 
System.out.println(Arrays.toString(numbers));

As you can see, lambda expressions make code is much more concise. Lambda expressions make code much easier to write and maintain and have relatively few drawbacks. The only drawback is that the syntax may seem somewhat cryptic to new users. After a little bit of time with lambdas, though, it becomes natural. Developers will wonder how they ever managed to write code without them.

The code for these Lambda expressions in Java example can be found on GitHub.

b