News Stay informed about the latest enterprise technology news and product updates.

What is a lambda expression and from where did the term 'lambda' elute?

It was Java 8 that saw the inclusion of lambda expressions, a concept that has long been baked into languages such as Groovy and Ruby, but due to various language constraints, had never made it into the Java language. As organizations slowly move to Java 8 and Java 11 platforms, more and more developers are getting the opportunity to use lambda functions in their code, which generates a great deal of both excitement, confusion and questions, with one of the most common questions being ‘why is this new language feature called a lambda function?”

Why are they called ‘lambda expressions’?

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, with a lambda function in math, a single set of variables are mapped onto a function and a result gets generated. The corollary to computer science is taking a set of variables and mapping those variables to a single function, with extra emphasis being placed on the word single. Lambdas work when there is only a single function to be implemented. The concept of a lambda completely falls apart in the world of 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 expressions in Java

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

lambda function in Java

Using a lambda function in a Java program.

In Java, there are innumberable places in which a piece of code needs to be provided a single method implementation. Interfaces abound 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 so they can be easily sorted. 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;


When using 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);
lambda expression code bloat

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

By the way, when learning to use lambda expressions, it is sometimes easier to assign the lambda expression to the interface it is implementing in a separate step. The prior example might read a bit clearer if code with an additional line:

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

As you can see, when using lambda functions, the code is much more concise, and bloat is minimized. Lambdas makes code much easier to write and easier to maintain, and there are relatively few drawbacks to using them. The only drawback is that the syntax may seem somewhat cryptic to new users, but after a little bit of time spent using lambdas, it becomes natural, and developers will wonder how they ever managed to write code without them.

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

Start the conversation

Send me notifications when other members comment.

Please create a username to comment.