In this article, we will discuss some important and frequently asked Java 8 Functional Interface Interview Questions and Answers.
Check out Java 8 Interview Questions
1. What is a functional interface?
An Interface that contains exactly one abstract method is known as a functional interface. It can have any number of default, static methods but can contain only one abstract method. It can also declare the methods of the object class.
Functional Interface is also known as Single Abstract Method Interfaces or SAM Interfaces. A functional interface can extend another interface only when it does not have any abstract method.
Java 8 provides predefined functional interfaces to deal with functional programming by using lambda and method references.
For example:
interface Printable {
void print(String msg);
}
public class JLEExampleSingleParameter {
public static void main(String[] args) {
// with lambda expression
Printable withLambda = (msg) -> System.out.println(msg);
withLambda.print(" Print message to console....");
}
}
Output : Print message to console....
interface Printable {
void print(String msg);
}
public class JLEExampleSingleParameter {
public static void main(String[] args) {
// with lambda expression
Printable withLambda = (msg) -> System.out.println(msg);
withLambda.print(" Print message to console....");
}
}
Print message to console....
Read more at Java 8 Functional Interfaces with Examples.
2. Is it possible to define our own Functional Interface? What is @FunctionalInterface? What are the rules to define a Functional Interface?
Yes, it is possible to define our own Functional Interfaces. We use Java 8 provides the @FunctionalInterface annotation to mark an interface as a Functional Interface.
If you use @FunctionalInterface annotation then the compiler will verify if the interface actually contains just one abstract method or not. It’s like the @Override annotation, which prevents you from accidental errors.
We need to follow these rules to define a Functional Interface:
- Define an interface with one and only one abstract method.
- We cannot define more than one abstract method.
- Use @FunctionalInterface annotation in the interface definition.
- We can define any number of other methods like default methods, static methods.
If your functional interface meets the above rules then you no need to use @FunctionalInterface annotation because the compiler will treat it as a functional interface.
The below example illustrates defining our own Functional Interface:
Let's create a Sayable interface annotated with @FunctionalInterface annotation.
@FunctionalInterface
interface Sayable{
void say(String msg); // abstract method
}
Let's demonstrate a custom functional interface via the main() method.
public class FunctionalInterfacesExample {
public static void main(String[] args) {
Sayable sayable = (msg) -> {
System.out.println(msg);
};
sayable.say("Say something ..");
}
}
Read more at Java 8 Functional Interfaces with Examples.
3. Name some of the functional interfaces in the standard library
In Java 8, there are a lot of functional interfaces are introduced in the java.util.function package and the more common ones include but are not limited to:
- Function – it takes one argument and returns a result
- Consumer – it takes one argument and returns no result (represents a side effect)
- Supplier – it takes no argument and returns a result
- Predicate – it takes one argument and returns a boolean
- BiFunction – it takes two arguments and returns a result
- BiConsumer - it takes two (reference type) input arguments and returns no result
- BinaryOperator – it is similar to a BiFunction, taking two arguments and returning a result. The two arguments and the result are all of the same types
- UnaryOperator – it is similar to a Function, taking a single argument and returning a result of the same type
- Runnable: use to execute the instances of a class over another thread with no arguments and no return value.
- Callable: use to execute the instances of a class over another thread with no arguments and it either returns a value or throws an exception.
- Comparator: use to sort different objects in a user-defined order
- Comparable: use to sort objects in the natural sort order
For more on functional interfaces, see the article at Java 8 Functional Interfaces with Examples.
4. What Is the Difference Between a Normal and Functional Interface in Java?
The normal interface in Java can contain any number of abstract methods, while the functional interface can only contain just one abstract method.We need to follow these rules to define a Functional Interface:
- Define an interface with one and only one abstract method.
- We cannot define more than one abstract method.
- Use @FunctionalInterface annotation in the interface definition.
- We can define any number of other methods like default methods, static methods.
5. What is a Function interface?
The Function is a functional interface introduced in Java 8; it takes an argument (object of type T) and returns an object (object of type R). The argument and output can be a different types.
This is the internal implementation of the Function interface:
@FunctionalInterface
public interface Function<T, R> {
R apply(T t);
}
- T – Type of the input to the function.
- R – Type of the result of the function.
Example:
Function < String, Integer > function = (t) -> t.length(); System.out.println(function.apply("Ramesh"));
Output:
6
Read more at Java 8 Function Interface Example
6. What is a Predicate interface?
The Predicate is a functional interface that can be used as an assignment target for a lambda expression.The Predicate interface represents an operation that takes a single input and returns a boolean value.
This is the internal implementation of the Predicate interface:
@FunctionalInterface
public interface Predicate<T> {
/**
* Evaluates this predicate on the given argument.
*
* @param t the input argument
* @return {@code true} if the input argument matches the predicate,
* otherwise {@code false}
*/
boolean test(T t);
}
T – Type of the input to the predicate
Example:
Predicate < Integer > predicate = (t) -> { if (t % 2 == 0) { return true; } else { return false; } }; System.out.println(predicate.test(10));
Output:
true
Read more at Java 8 Predicate interface Example
7. What is the Consumer interface?
A Consumer is a functional interface in JDK 8, which represents an operation that accepts a single input argument and returns no result.This is the internal implementation of the Consumer interface:
@FunctionalInterface
public interface Consumer < T > {
/**
* Performs this operation on the given argument.
*
* @param t the input argument
*/
void accept(T t);
}
Example:
Consumer < String > consumer = (t) -> System.out.println(t); consumer.accept("Ramesh");
Output:
Ramesh
Read more at Java 8 Consumer Interface Example
8. What is the Supplier interface?
The Supplier is a functional interface that represents an operation that takes no argument and returns a result.
This is the internal implementation of the Supplier interface:
@FunctionalInterface
public interface Supplier<T> {
T get();
}
Example:
Supplier < LocalDateTime > supplier = () -> LocalDateTime.now(); System.out.println(supplier.get());
Output:
2020-04-30T11:32:51.628
9. What is BiFunction interface?
The BiFunction interface is a functional interface that represents a function that takes two arguments of different types and produces a result of another type.
This is the internal implementation of the BiFunction interface:
@FunctionalInterface
public interface BiFunction<T, U, R> {
R apply(T t, U u); // Other default and static methods
// ...
}
Example:
// And with a lambda expression: BiFunction < Integer, Integer, Integer > biFunction = (t, u) -> (t + u); BiFunction < Integer, Integer, Integer > substraction = (t, u) -> (t - u); BiFunction < Integer, Integer, Integer > multiplication = (t, u) -> (t * u); BiFunction < Integer, Integer, Integer > division = (t, u) -> (t / u); System.out.println(biFunction.apply(10, 20)); System.out.println(substraction.apply(200, 100)); System.out.println(multiplication.apply(200, 100)); System.out.println(division.apply(200, 100));
Output:
30
900
20000
2
Read more at Java 8 BiFunction Example
Related Java Interview Articles
- Spring Boot Interview Questions
- Java Tricky Coding Interview Questions
- Java String Interview Questions
- Java String Tricky Coding Questions
- Java main() Method Interview Questions
- Java 8 Interview Questions
- Top 10 Spring MVC Interview Questions
- Java OOPS Tricky Coding Questions
- Java Programs Asked in Interview
- OOPS Interview Questions and Answers
- Hibernate Interview Questions
- JPA Interview Questions and Answers
- Java Design Patterns Interview Questions
- Spring Core Interview Questions
- Java Exception Handling Interview
Free Spring Boot Tutorial | Full In-depth Course | Learn Spring Boot in 10 Hours
Watch this course on YouTube at Spring Boot Tutorial | Fee 10 Hours Full Course
Comments
Post a Comment