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.
Example:
Java
// Java Program to Illustrate Functional Interface // Importing required classes import java.io.*; // Interface @FunctionalInterface interface display { // Attribute void show(String msg); // Method // To compute the sum default int doSum( int a, int b) { return a + b; } } // Main class implementing the above interface class GFG implements display { // Overriding the existing show() method @Override public void show(String msg) { // Print message System.out.println(msg); } // Main driver method public static void main(String[] args) { // Creating object of class inside main() GFG object = new GFG(); // Calling show() method in main() object.show( "Hello World!" ); // Print statement System.out.println(object.doSum( 2 , 3 )); } } |
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
Java
// Java Program to Illustrate Functional Interface // With the Usage of Anonymous class // Importing input output classes import java.io.*; // Interface @FunctionalInterface interface Cab { void bookCab(); } // Main class class GFG { // Method 1 // Main driver method public static void main(String[] args) { // Creating object of above functional interface Cab cab = new Cab() { // Method 2 // Overriding above bookCab() method @Override public void bookCab() { // Print statement System.out.println( "Booking Successful!! Arriving Soon!!" ); } }; cab.bookCab(); } } |
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
// Java Program Without Using Lambda Expression // Importing all utility classes import java.util.*; // Main class class GFG { // main driver method public static void main(String[] args) { // Creating an List of integer type List<Integer> list = Arrays.asList( 234 , 780 , 451 , 639 , 456 , 98 , 75 , 43 ); // Printing List before sorting System.out.println( "Before Sorting" ); for ( int i : list) System.out.print(i + " " ); System.out.println(); // Comparator is a functional interface // which is helps to sort object Collections.sort(list, new Comparator<Integer>() { // Overriding the existing compare method @Override public int compare(Integer a1, Integer a2) { return a1 % 10 > a2 % 10 ? 1 : - 1 ; } }); // Printing the list after sorting System.out.println( "After Sorting" ); for ( int i : list) System.out.print(i + " " ); System.out.println(); } } |
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(); } } |
Welcome
Example 2: Lambda Expression with Multiple Parameter
Java
// Java Program to Illustrate Lambda Expression // with Multiple Parameter // Importing required classes import java.util.*; // Main class class GFG { // main driver method public static void main(String[] args) { // Creating a List of integer type List<Integer> list = Arrays.asList( 24 , 346 , 78 , 90 , 21 , 765 ); // Printing before sorting System.out.println( "Before sorting." ); for ( int i : list) System.out.print(i + " " ); System.out.println(); // Sort the integers based on second digit Collections.sort(list, (a1, a2) -> { return a1 % 10 > a2 % 10 ? 1 : - 1 ; }); // Printing after sorting System.out.println( "After sorting." ); for ( int i : list) System.out.print(i + " " ); System.out.println(); } } |
Before sorting. 24 346 78 90 21 765 After sorting. 90 21 24 765 346 78
Please Login to comment...