What exactly lambda expression is?!
Feature that coming in Java 8 is more useful for every programmer as they write much more readable code. To be honest, it is difficult to understand the lambda expression at first, but after reading this article, these problems will disappear. So without further ado let’s get started.
Before using lambda expressions, you need to know what functional interfaces and abstract methods are. Simply put, a functional interface is nothing but including of an only one abstract method whose object cannot be created. We programmers create an interface and abstract method and then implement it in classes.
As we know before Java 8 we were implement interface in two ways. We were implement this by either creating a concrete class or directly creating the interface object (Anonymous Classes by the jvm). These examples are given in the photos below.
as you can see, we have used the abstract method of this interface by creating a concrete class here. Now let’s look at an example that uses the abstract method of this interface by creating anonymous classes on the jvm side.
as you can see, we used the abstract method of our interface without creating any classes. In fact, a class is created here and the runtime is created by that jvm and the name of the class is “Main$1”.
But wait, the two way that you have just seen are extremely redundant. Can we use the abstract method of this interface without creating any class? Of course yes!!! We can use this with the help of lambda expression. How Does? Let’s see together.
The most important way to understand lambda expression, we know that before Java 8, we cannot create implementation of any method body within a method or we cannot pass an implementation of method as an argument into another method. But with lambda expression we can do this fortunately. I have shown these two differences in the examples below.
but we can solve this syntax or compile time problem with lambda expression. Look at example in the following picture.
Lambda expression nothing but anonymous method. As you can see we can create a method implementation of body inside of bar() method.Now let’s understand what the syntaxis structure we used here is.
The picture above is definitely the same code as in the picture below.
Now that we understand what lambda expressions are, let’s try to understand how to use lambda expressions.
- Before using Lambda expression, the first thing you need to do is to check which functional interface the method uses. For example, if I need to explain the problem simply, let’s look at the Consumer<T> functional interface.
As you can see, this is a functional interface and there is only one abstract method called accept().
2. Later, if we want to use accept() method as a lambda expression, we can copy this method and write a comment on the method we use and write its use there. For example:
as you can see there is a method that is called forEach() in the Stream API that accept Consumer<T> functional interface as an argument. We can use this method with the lambda expression because it accept functional interface.
3. The next step we will do is to look at the syntaxis structure of the method we just copied in the comment line and use it exactly like a lambda expression.
Thus, you can use the desired Stream API function as a lambda expression by looking at the rule explained in the photo above.