Lambda Expression in Java

Jintin
3 min readNov 26, 2017

--

Java has been criticized about its heavy syntax for years compare to other modern programming languages. But things changed since the new coming Java 8 where we have the ability to use new feature like lambda expression to reduce/improve our code. What is lambda and what it makes Java development a better world? Let’s start from a simple callback function example.

In this example, our intent is to add a callback function onClick into a view by setOnClickListener function. But in Java, every thing is an object, we have to wrap the onClick function into an object by OnClickListener interface. Here is the code example.

interface OnClickListener {
void onClick(View v);
}
view.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
clickView(v);
}
});

That is a totally unnecessary if we can get a legal function reference like other functional language, and that is why lambda is here to rescue.

Lambda function

The basic lambda structure is to describe functions in a different way:

(Type1 parameter1, Type2 parameter2 ...) -> { code block }

Through this format we can treat the whole lambda block as a parameter or a variable without conflict with origin function definition. So here is the lambda version of our previous code:

view.setOnClickListener((View v) -> { 
clickView(v)
});

MAGIC! Since OnClickListener has only one abstract function onClick (which we called ‘functional interface’ if there’s only one abstract function should be implement), Java 8 give us the ability to omit the function name and treat the lambda function as same as OnClickListener instance do. That is how the magic happened. That’s keep doing some improvement.

If the code block only contain one line we can omit the { and } as well.

view.setOnClickListener((View v) -> clickView(v));

And the type of v can also infer as the onClick function already declared.

view.setOnClickListener((v) -> clickView(v));

The ( and ) are also meaningless in our case. But one thing to note is if there’s no parameter, the bracket should be placed.

view.setOnClickListener(v -> clickView(v));

Straightforward. It is pretty much the final version of our lambda example, but still the v is kind of duplicate. Here comes another concept called ‘method reference’.

Method Reference

Method reference is a reference to a function through an object without parameter signature. The format is bellow:

object::function

The idea here we want to strip the parameter is because we want to omit the v in our previous lambda example code. That way the method train will not know how to transfer parameters, so we have to add one restriction for it: the parameter should be matched. The parameter numbers and types (super class is fine thanks to OOP) should be the same. The updated version will look like this:

view.setOnClickListener(this::clickView);

Another case is that if we still have only one function to execute and the first parameter is the object we want to execute. Then we can use Class name to indicate the parameter shift.

view.setOnClickListener(v -> v.doSomething());
view.setOnClickListener(View::doSomething);

Recap

  1. If you have a method parameter need a ‘functional interface’, you can use lambda function.
  2. If your lambda function only have one method to call and the parameters are matchable, you can use ‘method reference’.

Reference

--

--

Jintin

Android/iOS developer, husband and dad. Love to build interesting things to make life easier.