5.
Functional Interfaces
Interface which contains Only Single Abstract Method (SAM) is called Functional Interface.
You can mark the Functional Interface optionally with @FunctionalInterface
Functional Interface can have the following
o One Abstract Method
o Multiple default methods
o Multiple static methods
o Multiple private methods ( from Java9 )
o Multiple private static methods ( from Java9 )
You need a Functional Interface to write the Lambda Expressions.
You can define the Functional Interface in your own or you can make use of Built-In
Functional Interface
Following are the List of most important of Built-In Functional Interfaces
o Predicate
o BiPredicate
o Function
o UnaryOperator
o BiFunction
o BinaryOperator
o Consumer
o BiConsumer
o Suppiler
o Primitive Functional Interfaces
Java Learning Center 41 Java 8 New Features
Demo1: Files Required:
1. [Link] 2. [Link]
1)[Link]
package [Link];
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
@FunctionalInterface
public interface Hello {
void test(int a,int b) throws ArithmeticException;
2)[Link]
package [Link];
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
public class Demo1 {
public static void main(String[] args) {
Hello hello= (a,b) -> {
[Link]("Lambda Code Starts");
try {
int result = a/b;
[Link]("Result is "+ result);
}catch(Exception ex) {
[Link]();
}
[Link]("Lambda Code Ends");
};
[Link](50, 0);
}
}
Java Learning Center 42 Java 8 New Features
Demo2: Files Required:
1. [Link] 2. [Link]
1)[Link]
package [Link];
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
@FunctionalInterface
public interface Hello {
void test(int a,int b) throws ArithmeticException;
}
2)[Link]
package [Link];
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
public class Demo2 {
public static void main(String[] args) {
[Link]("main Begin");
Hello hello= (a,b) -> {
[Link]("Lambda Begin");
int result = a/b;
[Link]("Result is "+ result);
[Link]("Lambda End");
};
//[Link](50, 0);
try{
[Link](50, 0);
}catch(Exception ex) {
[Link]();
}
[Link]("main End");
}
}
Java Learning Center 43 Java 8 New Features
5.1. Predicate Functional Interfaces
Predicate Functional Interface
o Takes One Input Parameter
o Returns Boolean after processing.
Predicate Functional Interface has the following methods.
abstract boolean test(T); //SAM
static <T> Predicate<T> isEqual(Object); //Static
Predicate<T> negate(); //Default
Predicate<T> and(Predicate<? super T>);//Default
Predicate<T> or(Predicate<? super T>);//Default
Demo3: Files Required:
1. [Link]
[Link]
package [Link];
import [Link];
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
public class Demo3 {
public static void main(String[] args) {
Predicate<Integer> predicate1 = (num) -> {
[Link](num);
return num % 2 == 0;
};
boolean mybool = [Link](19);
[Link](mybool);
mybool = [Link](28);
[Link](mybool);
Predicate<Integer> predicate2 = (num) -> num % 2 != 0;
Java Learning Center 44 Java 8 New Features
mybool = [Link](19);
[Link](mybool);
mybool = [Link](28);
[Link](mybool);
}
}
Demo4: Files Required:
1. [Link]
[Link]
package [Link];
import [Link];
import [Link];
import [Link];
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
public class Demo4 {
public static void main(String[] args) {
Predicate<Integer> predicate1 = (num) -> {
[Link](num);
return num % 2 == 0;
};
Predicate<Integer> predicate2 = (num) -> num % 2 != 0;
List<Integer> mylist1 = new ArrayList<>();
[Link](20); [Link](21); [Link](22); [Link](23);
[Link](24); [Link](25); [Link](26);
[Link](mylist1);
[Link](predicate1);
[Link](mylist1);
[Link]("-------------------------");
List<Integer> mylist2 = new ArrayList<>();
[Link](20); [Link](21); [Link](22); [Link](23);
[Link](24); [Link](25); [Link](26);
Java Learning Center 45 Java 8 New Features
[Link](mylist2);
[Link](predicate2);
[Link](mylist2);
}
}
Demo5: Files Required:
1. [Link]
[Link]
package [Link];
import [Link];
import [Link];
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
public class Demo5 {
public static void main(String[] args) {
List<Integer> mylist1 = new ArrayList<>();
[Link](20); [Link](21); [Link](22); [Link](23);
[Link](24); [Link](25); [Link](26);
[Link](mylist1);
[Link]((num) -> num %2 ==0); //IMP
[Link](mylist1);
[Link]("-------------------------");
List<Integer> mylist2 = new ArrayList<>();
[Link](20); [Link](21); [Link](22); [Link](23);
[Link](24); [Link](25); [Link](26);
[Link](mylist2);
[Link]((num) -> num %2 !=0); //IMP
[Link](mylist2);
}
}
Java Learning Center 46 Java 8 New Features
Demo6: Files Required:
1. [Link]
[Link]
package [Link];
import [Link];
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
public class Demo6 {
public static void main(String[] args) {
Predicate<String> predicate1 = [Link]("Hello Guys");
boolean mybool = [Link]("Hello Guys");
[Link](mybool);
mybool = [Link]("Hai Guys");
[Link](mybool);
Predicate<Integer> predicate2 = [Link](99);
mybool = [Link](99);
[Link](mybool);
mybool = [Link](88);
[Link](mybool);
[Link]("--------------");
Predicate<Integer> predicate3 = (num) -> num % 2 == 0;
Predicate<Integer> predicate4 = (num) -> num % 2 != 0;
mybool = [Link](28);
[Link](mybool); //T
mybool = [Link]().test(28);
[Link](mybool); //F
mybool = [Link](19);
[Link](mybool); //T
mybool = [Link]().test(19);
[Link](mybool); //F
}
}
Java Learning Center 47 Java 8 New Features
Demo7: Files Required:
1. [Link]
[Link]
package [Link];
import [Link];
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
public class Demo7 {
public static void main(String[] args) {
Predicate<Integer> predicate1 = (num) ->{
[Link]("Predicate 1");
return num % 2 == 0;
};
Predicate<Integer> predicate2 = (num) -> {
[Link]("Predicate 2");
return num % 2 != 0;
};
Predicate<Integer> predicate3 = (num) -> {
[Link]("Predicate 3");
return num >= 25 && num <= 50;
};
// Check whether Number is Even and between 25 and 50
boolean mybool = [Link](predicate3).test(28);
[Link](mybool);
mybool = [Link](predicate3).test(19);
[Link](mybool);
// Check whether Number is Odd and between 25 and 50
mybool = [Link](predicate3).test(29);
[Link](mybool);
mybool = [Link](predicate3).test(19);
[Link](mybool);
Java Learning Center 48 Java 8 New Features
// Check whether Number is Even or between 25 and 50
mybool = [Link](predicate3).test(29);
[Link](mybool);
mybool = [Link](predicate3).test(28);
[Link](mybool);
mybool = [Link](predicate3).test(18);
[Link](mybool);
// Check whether Number is Odd or between 25 and 50
mybool = [Link](predicate3).test(29);
[Link](mybool);
mybool = [Link](predicate3).test(28);
[Link](mybool);
mybool = [Link](predicate3).test(18);
[Link](mybool);
Interview Questions:
Q1) What is a Function Interface?
Ans:
Q2) What is the annottaion to mark on the Function Interface?
Ans:
Q3) Is it mandatory to mark @FunctionalInterface?
Ans:
Java Learning Center 49 Java 8 New Features
Q4) Can I write two abstract methods in One Function Interface?
Ans:
Q5) Can I have default methods in Function Interface?
Ans:
Q6) Can I have static methods in Function Interface?
Ans:
Q7) Can I have private methods in Function Interface? (Java 9)
Ans:
Q8) Can I have private static methods in Function Interface? (Java 9)
Ans:
Q9) Can I write Lambda exp without Function Interface?
Ans:
Q10) Can we change the return type or parameters of SAM during implementation?
Ans:
Q11) How can I throws exception at method level using Functional Interface?
Ans:
Q12) What are the built-IN functional Interfaces?
Ans:
Java Learning Center 50 Java 8 New Features
Q13) Which of the following Hello are Valid Function Interfaces?
1)
interface Hello {
}
2)
interface Hello {
void show();
}
3)
interface Hai {
void show();
}
Interface Hello extends Hai{
}
4)
interface Hello {
void show();
default void display(){
[Link](“OK”);
}
5)
interface Hello {
void m1();
void m2();
}
Java Learning Center 51 Java 8 New Features