Lambda expression is not difficult

Tram Ho

Java 8 adds a lot of features, including lambda expression syntax. Simply put, this is a more concise way of implementing a functional interface than using a class or anonymous class. You can review the overview in lesson 2.

Regarding the lambda syntax, in the previous posts, I have a rough example. And today I will go deeper into it.

1. General Syntax

The lambda syntax is quite similar to how an arrow function is written in JavaScript. It also includes 3 parts as follows:

  • Parameter list
  • Arrow token (sign ->)
  • The body contains the code

Take this code as an example.

Then the following part is a lambda expression (because it is an expression, so it can be assigned to a variable).

It is also important to note that the form of the lambda must match the method of the functional interface. Operation example filter() need a Predicate<T> which you give lambda form Function<T, R> it’s bad

2. Shorten lambda

Java’s compiler is getting smarter and smarter, so it can infer some type inference. Therefore, we do not need to write the full form for the lambda as above, but we can reduce it as follows.

First, you can drop the data type of the lambda param as follows.

But how does Java know? name what kind? In the first code, we have a list of List<String>, so the stream retrieved is Stream<String>. It can be deduced from that T to be String, so name also String.

Second, if the lambda has only 1 param as above, remove the parentheses () as follows.

Finally, consider removing the curly braces {} go now. Used when the body has only one command, many commands must be surrounded by curly braces.

From Java 10 it is possible to use var instead of letting Java infer the implicit type as above.

Oh but why use more var what to do, leave it blank it can also deduce it? Well, the reason is because if the lambda param has extra modifiers (like final) or annotation, the type cannot be left blank. Then use var Instead, it’s both type-inferred and error-free.

3. Capturing lambda?

Inside the body of the lambda you can declare normal local variables (like in methods). In addition, lambda has access to external variables.

Because of how lambda works, people divide lambda into 2 types:

  • Non-capturing: lambda has no access to any variables outside of it
  • Capturing: lambda has access to external variable

The example code above is capturing lambda, and the captured variable is increment.

And does Java apply the rule on the external variable captured by the lambda. That the capture variable needs to be final or effectively final (don’t know how to translate).

In general, the lambda is not allowed to modify the variable it captures.

Previously, with anonymous classes, it was mandatory final, but lambda is better than allowed effectively final (no need final, but do not edit that variable).


Okay post here. In short, you already know how to use the basic lambda. If you are interested in learning more about lambda, how it works, is lambda syntax sugar of anonymous class, … look forward to the next article.

As always, the article is also posted on my personal blog https://tonghoangvu.hashnode.dev/lambda-expression-khong-kho.

Share the news now