Match Lambdas to Interfaces in Java
Last Updated :
17 Jan, 2022
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.
Example:
Java
import java.io.*;
@FunctionalInterface
interface display {
void show(String msg);
default int doSum( int a, int b) { return a + b; }
}
class GFG implements display {
@Override
public void show(String msg)
{
System.out.println(msg);
}
public static void main(String[] args)
{
GFG object = new GFG();
object.show( "Hello World!" );
System.out.println(object.doSum( 2 , 3 ));
}
}
|
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
Java
import java.io.*;
@FunctionalInterface
interface Cab {
void bookCab();
}
class GFG {
public static void main(String[] args)
{
Cab cab = new Cab() {
@Override public void bookCab()
{
System.out.println(
"Booking Successful!! Arriving Soon!!" );
}
};
cab.bookCab();
}
}
|
Output
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.
Syntax:
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.
Example
Java
import java.util.*;
class GFG {
public static void main(String[] args)
{
List<Integer> list = Arrays.asList(
234 , 780 , 451 , 639 , 456 , 98 , 75 , 43 );
System.out.println( "Before Sorting" );
for ( int i : list)
System.out.print(i + " " );
System.out.println();
Collections.sort(list, new Comparator<Integer>() {
@Override
public int compare(Integer a1, Integer a2)
{
return a1 % 10 > a2 % 10 ? 1 : - 1 ;
}
});
System.out.println( "After Sorting" );
for ( int i : list)
System.out.print(i + " " );
System.out.println();
}
}
|
Output
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
Java
import java.io.*;
interface Display{
void show();
}
class GFG{
public static void main(String[] args){
Display display= ()->System.out.println( "Welcome" );
display.show();
}
}
|
Example 2: Lambda Expression with Multiple Parameter
Java
import java.util.*;
class GFG {
public static void main(String[] args)
{
List<Integer> list
= Arrays.asList( 24 , 346 , 78 , 90 , 21 , 765 );
System.out.println( "Before sorting." );
for ( int i : list)
System.out.print(i + " " );
System.out.println();
Collections.sort(list, (a1, a2) -> {
return a1 % 10 > a2 % 10 ? 1 : - 1 ;
});
System.out.println( "After sorting." );
for ( int i : list)
System.out.print(i + " " );
System.out.println();
}
}
|
Output
Before sorting.
24 346 78 90 21 765
After sorting.
90 21 24 765 346 78