An Interface that contains exactly one abstract method is known as functional interface. It can have any number of default and static methods but can contain only one abstract method. Functional Interface is the approach of functional programming in Java. It is also known as SAM interfaces Single Abstract Method Interfaces.
/*@FunctionalInterface*/
//It is optionalpublic interface MultipleInterface {
public int multiple(int x,int y); } public class Calculate{ public static void main (String
[] args) {
//with lambdaMultipleInterface m = (x, y) -> { return x * y; }; int multipleVal = m.multiple(10,30); System.out.println( "Multiple Value is "+multipleVal); } } Output: Multiple Value is 300
@FunctionalInterface annotation is optional but it is used to ensure that the functional interface can’t have more than one abstract method. If you add more than one abstract methods, an "Unexpected @FunctionalInterface annotation" message you can get. It is not mandatory to use this annotation.
//It is optional@FunctionalInterface interface Area { int calculateArea(int x); } class Square { public static void main (
String
[] args) { //with lambda Area a = (x) -> { return x * x; }; int areaVal = a.calculateArea(20); System.out.println( "Area of Square is: "+areaVal); } } Output: Area of Square is: 400
The consumer interface of the functional interface accepts only one single argument without any return value. The consumer interface has no return value. It contains an abstract accept() and a default andThen() method. It can be used as the assignment target for a lambda expression or method reference. details..
import java.util.function.Consumer; public class ConsumerInterfaceExample{ static void welcomeGreeting (
String
name){ System.out.println( "Welcome "+name); } static void showAge (int age){ System.out.println( "Your age is "+age); } static void showSalary (int sal){ System.out.println( "Your salary is "+sal); } public static void main(
String
[] args) { //method reference
Consumer<
String>
consumer1 = ConsumerInterfaceExample :: welcomeGreeting; // Calling Consumer method consumer1.accept("Tejas"); //method reference
Consumer<Integer>
consumer2 = ConsumerInterfaceExample :: showAge; // Calling Consumer method
consumer2.accept(30); //method reference
Consumer<
Integer>
consumer3 = ConsumerInterfaceExample :: showSalary; // Calling Consumer method
consumer3.accept(20000); } } Output: Welcome Tejas Your age is 30 Your salary is 20000
The different between Consumer and Bi-Consumer is Consumer takes only one single argument, but Bi-Consumer interface takes two arguments. Both, Consumer and Bi-Consumer have no return value. It is used in iterating through the entries of the map.
BiConsumer
<String
,String
> foo = (a, b) -> System.out.println( a + b );
Predicate accepts an argument and returns a boolean value. Predicate functional interface in Java is a type of function that accepts a single argument and returns a boolean (True/ False). It provides the functionality of filtering, it filters a stream components on the base of a provided predicate. details..
public static void main (
String
[] args) { Predicate<
Integer
> pr = a -> (a > 10); // Calling Predicate method System.out.println(pr.test(20)); } Output: true
public static void main (
String
[] args){ Predicate<
String
> pr = Predicate.isEqual( "MyAllText" ); // Calling Predicate method System.out.println( pr.test( "AllText" ) ); // Calling Predicate method System.out.println( pr.test( "MyAllText" ) ); // Calling Predicate method System.out.println( pr.test( "MyAll" ) ); } } Output: false true false
public static void main(
String
[] args) { Predicate<
Integer
> pr1 = x -> (x > 20); Predicate<
Integer
> pr2 = x -> (x < 80); // Calling Predicate method System.out.println( pr1.and(pr2).test(100) ); // Calling Predicate method System.out.println( pr1.and(pr2).test(50) ); // Calling Predicate method System.out.println( pr1.and(pr2).test(10) ); } Output: false true false
public static void main(
String
[] args) { Predicate<
String
> startsWithCharJ = s -> s.startsWith("H"); Predicate<
String
> hasLengthOfInt5 = s -> s.length() == 5; // Calling without negate method System.out.println( startsWithCharJ.test( "Horse" ) ); // Calling without negate method System.out.println( hasLengthOfInt5 .test( "India" ) ); Predicate<
String
> negateStartsWithCharJ = startsWithCharJ.negate(); Predicate<
String
> negatehasLengthOfInt5 = hasLengthOfInt5.negate(); // Calling after negate predicate System.out.println( negateStartsWithCharJ .test( "Horse" ) ); // Calling after negate predicate System.out.println( negatehasLengthOfInt5 .test( "India" ) ); } Output: true true false false
public static void main (
String
[] args) { Predicate<
String
> startsWithCharH = s -> s.startsWith("H"); Predicate<
String
> hasLengthOfInt5 = s -> s.length() == 5; Predicate<
String
> startsWithCharHOrHasLengthOf5 = startsWithCharH.or(hasLengthOfInt5); System.out.println( startsWithCharHOrHasLengthOf5 .test( "Hero" ) ); System.out.println( startsWithCharHOrHasLengthOf5 .test( "India" ) ); System.out.println( startsWithCharHOrHasLengthOf5 .test( "Method" ) ); } Output: true true false
Bi-Predicate is also an extension of the Predicate functional interface, which, instead of one, takes two arguments, does some processing, and returns the boolean value.
The function type functional interface receives a single argument, processes it, and returns a value. Functional interface is taking the key from the user as input and searching for the value in the map for the given key. details..
public static void main (
String
[] args) { private static HashMap<
Integer
,
String
> Employee = new HashMap<>(); Function<
Integer
,
String
> addFunc = (Integer ID)-> { if(Employee.containsKey(ID)) { return Employee.get(ID); }else{ return "Invalid ID"; } }; // Returns the Employee object if present in the hashmap or return Invalid Id message System.out.println( addFunc.apply( 500 )); };
Bi-function is just like a function, it takes two arguments. Two arguments are must in Bi-function. Just like a function it also returns a value.
The Supplier functional interface is also a type of functional interface that does not take any input or argument and returns a single output. The Supplier interface takes only one generic type, the type of data it is going to return. get() is the abstract method of the Supplier. details..
public static void main (
String
[] args) { Supplier<
String
> supp = () -> "Hello World"; System.out.println( supp.get() ); }; Output: Hello World