Implement functional interfaces using lambda expressions
Java is Object Oriented Programming language, being object-oriented is not bad, but it brings a lot of verbosity to the program.Java 8 introduced new libraries and programming styles, for example, functional interfaces, lambda expressions, and streams. These bring functional-style programming to the object-oriented programming capabilities of Java.
Java Functional Interface and Lambda Expression help us in writing smaller and cleaner code by removing a lot of boilerplate code.
Java Functional Interface:
- A functional interface in Java is an interface that contains only a single abstract method.
- A functional interface can contain default and static methods which do have an implementation, in addition to the single unimplemented method.
Here is a Java functional interface examples:
Example 1,
The below counts as a functional interface in Java because it only contains a single method, and that method has no implementation:
public interface Addition {
public Integer calculate(Integer num1, Integer num2);
}
Example 2,
If we use @FunctionalInterface, it makes sure there should be only one abstract method within that interface, otherwise will get a compiler error:
@FunctionalInterface
public interface Addition {
public Integer calculate(Integer num1, Integer num2);
}
Example 3,
The below interface still counts as a functional interface in Java, since it only contains a single non-implemented method:
public interface Addition {
public Integer calculate(Integer num1, Integer num2);
public default void newMethod() {
System.out.println("Newly added default method in Interface");
}
public static void anotherNewMethod() {
System.out.println("Newly added static method in Interface");
}
}
Java built-in functional interfaces,
Since Java 1.8 onward following interfaces are functional interfaces.
1. Runnable:
public abstract interface Runnable {
public abstract void run();
}
It contains only run() method.
2. Comparable:
public abstract interface Comparable<T> {
public abstract int compareTo(T arg0);
}
It contains only compareTo() method.
3. ActionListener:
public abstract interface ActionListener
extends java.util.EventListener {
public abstract void
actionPerformed(java.awt.event.ActionEvent arg0);
}
It contains only actionPerformed() method.
4. Callable:
public abstract interface Callable<V> {
public abstract V call() throws java.lang.Exception;
}
It contains only call() method.
5. The java.util.function package defines several in-built functional interfaces that can be used when creating lambda expressions or method references.
Functional Interface Can Be Implemented By A Lambda Expression
Example 1: Java Lambda expression to write a Comparator to sort a List.
Sort without Lambda:
package demo;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
public class Demo {
public static void main(String[] args) {
List<User> listOfUser = new ArrayList<User>();
listOfUser.
add(new User("sibin", 3333.23, "sibin@gmail.com"));
listOfUser.
add(new User("sibin2", 11111.23, "sibin@gmail2.com"));
listOfUser.
add(new User("sibin1", 2222.23, "sibin@gmail1.com"));
Comparator<User> sortBySalary = new Comparator<User>() {
@Override
public int compare(User arg0, User arg1) {
return arg0.getSalary().compareTo(arg1.getSalary());
}
};
listOfUser.sort(sortBySalary);
System.out.print(listOfUser);
}
}
Sort with Lambda:
package demo;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
public class Demo {
public static void main(String[] args) {
List<User> listOfUser = new ArrayList<User>();
listOfUser.
add(new User("sibin", 3333.23, "sibin@gmail.com"));
listOfUser.
add(new User("sibin2", 11111.23, "sibin@gmail2.com"));
listOfUser.
add(new User("sibin1", 2222.23, "sibin@gmail1.com"));
Comparator<User> sortBySalary = (User arg0, User arg1)->{
return arg0.getSalary().compareTo(arg1.getSalary());
};
listOfUser.sort(sortBySalary);
System.out.print(listOfUser);
}
}
Example 2: Custom Functional Interface.
Create a Functional Interface
package demo;
public interface Addition {
public Integer calculate(Integer num1, Integer num2);
}
Demo
package demo;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
public class Demo {
public static void main(String[] args) {
Addition sum = (Integer num1, Integer num2) -> num1 + num2;
System.out.println(sum.calculate(4, 8));
}
}
Example 3: Predicate Functional Interface example
The predicate is a single argument functional interface that returns true or false. It takes one argument and returns results in form of true or false.
package demo;
import java.util.function.Predicate;
public class Demo {
public static void main(String[] args) {
Predicate<String> nonEmptyStringPredicate =
s -> !s.isEmpty();
String name = "";
boolean result = nonEmptyStringPredicate.test(name);
System.out.println(result);
}
}
Example 4: Consumer Functional Interface example
Java Consumer is a functional interface that represents an operation that accepts a single input argument and returns no result.
package demo;
import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;
public class Demo {
public static void main(String[] args) {
List<String> names = Arrays.asList("Sibin", "zafin", "joy");
Consumer<String> makeUpperCase = (String t) ->
System.out.println(t.toUpperCase());
names.forEach(makeUpperCase);
}
}
Thank you!