@techwithvishalraj
Java
Functional
interface
Functional Interfaces
@techwithvishalraj
An Interface that contains exactly one abstract method is known
as functional interface.
Functional Interface is also known as Single Abstract Method
Interfaces or SAM Interfaces.
It is a new feature in Java, which helps to achieve functional
programming approach.
It can contain any number of Object class methods.
Java provides an anotation @FunctionalInterface, which is used to
declare an interface as functional interface.
@FunctionalInterface
interface Welcome{
void hello(); // abstract method
// It can contain any number of Object class methods.
int hashCode();
String toString();
boolean equals(Object object);
}
public class Test1 {
public static void main(String[] args) {
Welcome welcome = ()-> [Link]("Hey hii :) ");
[Link]();
[Link]([Link]());
}
} output: Hey hii :)
455659002
Functional Interfaces
@techwithvishalraj
It can have any number of default, static methods.
@FunctionalInterface
interface MyFunctionalInterface {
void abstractMethod();
default void defaultMethod() {
[Link]("Default method implementation");
}
static void staticMethod() {
[Link]("Static method implementation");
}
}
public class Test1 {
public static void main(String[] args) {
MyFunctionalInterface fi = ()->
[Link]("Abstarct method implementation");
[Link]();
[Link]();
[Link]();
}
}
output: Abstarct method implementation
Default method implementation
Static method implementation
Functional Interfaces
@techwithvishalraj
A functional interface can extends another interface only when it
does not have any abstract method but it can contain default, static
and object class methods.
interface MyInterface {
default void defaultMethod(){
[Link]("Default method implementation");
}
int hashCode();
}
@FunctionalInterface
interface MyFunctionalInterface extends MyInterface {
void abstractMethod();
}
public class Test3 {
public static void main(String[] args) {
MyFunctionalInterface fi = ()->
[Link]("Abstarct method implementation");
[Link]();
[Link]([Link]());
}
}
output: Abstarct method implementation
250421012
Predefined-Functional Interfaces
@techwithvishalraj
Java provides predefined functional interfaces to deal with
functional programming by using lambda and method references.
Following is the list of some functional interface which are placed
in [Link] package.
Interface Description
It represents an operation that accepts a single
Consumer<T>
argument ‘T’ and does not returns result.
It represents an operation that accepts two
BiConsumer<T,U>
input arguments and does not returns result.
It represents an operation that not take any
Supplier<T>
arguments and returns a result of type ‘T’.
represents operation that accepts a single
Predicate<T>
argument ‘T’ and return boolean value.
It represents a function that accepts one
Function<T,R>
argument ‘T’ and returns a result ‘R’.
It represents a function that accepts two
BiFunction<T,U,R>
arguments (‘T’, ‘U) and returns a result ‘R’.
@techwithvishalraj
Consumer<T> Interface
It contains an abstract accept() method and a default andThen() method.
Consumer<String> con = (name) -> [Link]("hello "+name);
[Link]("vishal"); -------------> hello vishal
BiConsumer<T,U> Interface
It contains an abstract accept() method and a default andThen() method.
BiConsumer<String, Integer> biConsumer = (name, age)->
[Link](name+ " : "+age);
[Link]("vishal", 24); ---------------> vishal : 24
Supplier<T> Interface
It has a single abstract method get().
Supplier<Double> supplier = ()-> [Link]()*100;
[Link]([Link]()); -----------> 58.17961621550302
@techwithvishalraj
Predicate<T> Interface
it contains an abstract test() method and some default & static
methods too.
Predicate<Integer> predicate = age-> age>18;
Sop([Link](12)); ---------> false
Function<T,R> Interface
it contains an abstract apply() method and some default & static
methods too.
Function<String, String> function = (name)-> "hello "+ name;
Sop([Link]("vishal")); ---------> hello vishal
BiFunction<T,U,R> Interface
it contains an abstract apply() method and default andThen() method.
BiFunction<String, String, String> biFunction = (s1, s2)-> s1+" "+s2;
Sop([Link]("hello", "vishal")); -------> hello vishal
@techwithvishalraj
Thank
you!
vishal-bramhankar
techwithvishalraj
Vishall0317