A functional interface is an interface that contains only one abstract method.
Along with the one abstract method, they can have any number of default and static methods.
In Java 8 there are four types of Functional Interfaces.
1) Predicate
2) Consumer
3) Function
4) Supplier
----------------------------------------------------------
1) Predicate gives only boolean value on the basis of one argument,
it has only one method called test().
class PredicateExample {
public static void main(String[] args) {
Predicate<String> checkLength = t->t.length()>3;
System.out.println(checkLength.test("Aman"));
}
}
// Result will be : true
-----------------------------------------------------------
2) Consumer only Consumes data and no output will be send.
It has only one method called accept().
class Employees{
String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public class ConsumerExample {
public static void main(String[] args) {
Employees e =new Employees();
Consumer<Employees> getNames = t->t.setName("Ajay Kumar Dargan");
getNames.accept(e);
System.out.println(e.getName());
}
}
// Result will be : Ajay Kumar Dargan
------------------------------------------------------------
3) Function it has two arguments like Function(T , R)
'T' represent as input and 'R' represent as output.
It has only one method called apply().
public class FunctionExample {
public static void main(String[] args) {
Function<Integer,String> multiply = m->m*10+" << Multiply by 10";
System.out.println(multiply.apply(20));
}
}
//Result : 200 << Multiply by 10
--------------------------------------------------------------
4) Supplier does not take any arguments, but it only gives output.
It has only one method called get().
public class SupplierExample {
public static void main(String[] args) {
Supplier<Double> getRendumNum = ()->Math.random();
System.out.println(getRendumNum.get());
}
}
Result : 0.1384031347391398
----------------------------------------------------------------
In Java 8 there are four types of Functional Interfaces.
1) Predicate
2) Consumer
3) Function
4) Supplier
----------------------------------------------------------
1) Predicate gives only boolean value on the basis of one argument,
it has only one method called test().
class PredicateExample {
public static void main(String[] args) {
Predicate<String> checkLength = t->t.length()>3;
System.out.println(checkLength.test("Aman"));
}
}
// Result will be : true
-----------------------------------------------------------
2) Consumer only Consumes data and no output will be send.
It has only one method called accept().
class Employees{
String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public class ConsumerExample {
public static void main(String[] args) {
Employees e =new Employees();
Consumer<Employees> getNames = t->t.setName("Ajay Kumar Dargan");
getNames.accept(e);
System.out.println(e.getName());
}
}
// Result will be : Ajay Kumar Dargan
------------------------------------------------------------
3) Function it has two arguments like Function(T , R)
'T' represent as input and 'R' represent as output.
It has only one method called apply().
public class FunctionExample {
public static void main(String[] args) {
Function<Integer,String> multiply = m->m*10+" << Multiply by 10";
System.out.println(multiply.apply(20));
}
}
//Result : 200 << Multiply by 10
--------------------------------------------------------------
4) Supplier does not take any arguments, but it only gives output.
It has only one method called get().
public class SupplierExample {
public static void main(String[] args) {
Supplier<Double> getRendumNum = ()->Math.random();
System.out.println(getRendumNum.get());
}
}
Result : 0.1384031347391398
----------------------------------------------------------------
No comments:
Post a Comment