Functional Interface
.What is Functional Interface?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
@FunctionalInterface public interface Decorable { // one abstract method void decorateWithCurtains(); // default method default void decorateWithPaints() { System.out.println("Decorating using paints"); } // Overriding method of java.lang.Object @Override public int hashCode(); } |
- java.lang.Runnable
- java.util.concurrent.Callable
- java.awt.event.ActionListener
- java.util.Comparator
Why Lambda Expressions?
Let’s say you need to sort list of movies by movie name.
Movie.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
package org.arpit.java2blog; public class Movie { String movieName; long duration; public Movie(String movieName, long duration) { super(); this.movieName = movieName; this.duration = duration; } // getters and setters } |
Code to sort list of movies by name using comparator
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
Movie m1=new Movie("Inception",110); Movie m2=new Movie("GodFather",200); Movie m3=new Movie("Forest Gump",130); Movie m4=new Movie("Avengers",150); List<Movie> listOfMovies = new ArrayList<>(); listOfMovies.add(m1); listOfMovies.add(m2); listOfMovies.add(m3); listOfMovies.add(m4); System.out.println("Before Sort by name : "); for (int i = 0; i < listOfMovies.size(); i++) { Movie movie = (Movie) listOfMovies.get(i); System.out.println(movie); } // Sort by movieName // Anonymous Comparator // old way Collections.sort(listOfMovies, new Comparator<Movie>() { @Override public int compare(Movie o1, Movie o2) { return o1.getMovieName().compareTo(o2.getMovieName()); } }); |
Anonymous classes
is syntax. For very simple operation, we need to write complex code. To solve this problem, JDK has introduced a new feature called Lambda Expressions. I will take this example after explaining lambda expression to understand, how lambda expression
will reduce this complex code.What is Lambda Expressions:
function without name
,Structure of Lambda Expressions
(Argument List) ->{statements;}

- Argument list or parameters
- Lambda expression can have zero or more arguments.
()->{System.out.println(“Hello”)}; //Without argument, will print hello
(int a)->{System.out.println(a)}; // One argument, will print value of a
(int a,int b)-> {a+b};//two argument, will return sum of these two integers - You can choose to not declare the type of arguments as it can be inferred from context.
-
(a,b)->{a+b}; // two argument, will return sum of these two numbers
- you can not declare one argument’s type and do not declare type for other argument.
-
(int a,b)->{a+b}; // Compilation error
- When there is a single parameter, if its type is inferred, it is not mandatory to use parentheses
a->{System.out.println(a)}; // Will print value of number a
- Lambda expression can have zero or more arguments.
- Array token (->)
- Body
Body
can have expression or statements.- If there is only one
statement
in body,curly brace is not needed and return type of the anonymous function is same as of body expression - If there are more than one
statements
, then it should be incurly braces
and return type of anonymous function is same as value return from code block,void
if nothing is returned.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
public class ThreadSample { public static void main(String[] args) { // old way new Thread(new Runnable() { @Override public void run() { System.out.println("Thread is started"); } }).start(); // using lambda Expression new Thread(()->System.out.println("Thread is started")).start(); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
package org.arpit.java2blog; public class Movie { String movieName; long duration; public Movie(String movieName, long duration) { super(); this.movieName = movieName; this.duration = duration; } public String getMovieName() { return movieName; } public void setMovieName(String movieName) { this.movieName = movieName; } public long getDuration() { return duration; } public void setDuration(long duration) { this.duration = duration; } @Override public String toString() { return "Movie Name: " + this.getMovieName() + "|| " + "Movie duration: " + this.getDuration(); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
package org.arpit.java2blog; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; public class ComparatorLambdaMain { /** * @author Arpit Mandliya */ public static void main(String[] args) { Movie m1=new Movie("Inception",110); Movie m2=new Movie("GodFather",200); Movie m3=new Movie("Forest Gump",130); Movie m4=new Movie("Avengers",150); List<Movie> listOfMovies = new ArrayList<>(); listOfMovies.add(m1); listOfMovies.add(m2); listOfMovies.add(m3); listOfMovies.add(m4); System.out.println("Before Sort by name : "); for (int i = 0; i < listOfMovies.size(); i++) { Movie movie = (Movie) listOfMovies.get(i); System.out.println(movie); } // Sort by movieName // Anonymous Comparator // old way Collections.sort(listOfMovies, new Comparator<Movie>() { @Override public int compare(Movie o1, Movie o2) { return o1.getMovieName().compareTo(o2.getMovieName()); } }); // Using lambda expression Collections.sort(listOfMovies, (o1, o2) -> o1.getMovieName().compareTo(o2.getMovieName())); System.out.println("After Sort by name: "); for (int i = 0; i < listOfMovies.size(); i++) { Movie movie = (Movie) listOfMovies.get(i); System.out.println(movie); } } } |
Movie Name: Inception|| Movie duration: 110
Movie Name: GodFather|| Movie duration: 200
Movie Name: Forest Gump|| Movie duration: 130
Movie Name: Avengers|| Movie duration: 150
After Sort by name:
Movie Name: Avengers|| Movie duration: 150
Movie Name: Forest Gump|| Movie duration: 130
Movie Name: GodFather|| Movie duration: 200
Movie Name: Inception|| Movie duration: 110
lambda expression
for using Comparator. So in spite of writing Anonymous comparator, our expression became very easy.
HelloWorld Lambda Expression Example
1 2 3 4 5 6 7 |
package org.arpit.java2blog; public interface HelloWorld { void sayHello(); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 |
package org.arpit.java2blog; public class HelloWorldMain { public static void main(String args[]) { // Lambda Expression HelloWorld helloWorld=()->System.out.println("Hello using Lambda Expression"); helloWorld.sayHello(); } } |
Excellent blogs for lambda expression.