Match Lambdas to Interfaces in Java
One of the most popular and important topics is lambda expression in java but before going straight into our discussion, let us have insight into some important things. Starting off with the interfaces in java as interfaces are the reference types that are similar to classes but containing only abstract methods. This is the definition of interface we had before java 1.8. As before java version 1.8, we are using any version, interfaces are of three types namely as listed below s follows:
- Normal Interface
- Interface with multiple methods.
- Marker interface (Interfaces does not contain any methods).
From 1.8 all SAM (Single Abstract Method) interfaces is called as Functional Interface.
Functional Interface: Functional interface is an interface that contains exactly one abstract method but the important point is to remember that it can have any number of default or static methods along with the object class methods, as this confuses programmers the most.
Hello World! 5
Furthermore, now let us discuss which is anonymous classes. Anonymous class is only creating for one-time use, we cannot reuse it. The question that must be wondering that why do we need such type of class? Some scenarios, like when our only purpose is to override a method you can use it. Now, another advantage of it is, with the help of this we can instantiate the interface very easily.
Example: Suppose you want to book a cab
Booking Successful!! Arriving Soon!!
Now is the right time to discuss lambda expressions in java. It is one of the important features of Java 8. In fact, Lambda expressions are the fundamental approach to functional programming in Java. It is an anonymous function that does not have a name and does not belong to any class This in itself is a big topic that has been discussed before. And you must have an idea about it before you know what we are talking about.
Now we are done with discussing all concepts prior to landing upon how can we match lambda expression with the interface?
- Lambda expression only works with functional interfaces in java.
- Lambda expression provides a clear and concise way to represent a method interface via an expression.
- It provides the implementation of a functional interface and simplifies software development.
- We can easily remove the boilerplate code with the help of the Lambda expression.
Parameter -> expression body
Let us see our code in a different way because we have already seen an anonymous class example before as here we create a list of integers and we want to sort them based on their last digit.
Before Sorting 234 780 451 639 456 98 75 43 After Sorting 780 451 43 234 75 456 98 639
Characteristics of Lambda Expression
- Optional type declaration
- Optional parenthesis around parameters.
- Optional curly braces for one line of code.
- Optional return keyword.
Lambda expression can take parameter just like method.
Example 1 Lambda expression with zero parameter
Example 2: Lambda Expression with Multiple Parameter
Before sorting. 24 346 78 90 21 765 After sorting. 90 21 24 765 346 78
Please Login to comment...