📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.
🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (176K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
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?
A functional interface in Java is an interface that has exactly one abstract method. Since functional interfaces have only one abstract method, they can represent a single functionality that can be implemented by a lambda expression, a method reference, or an anonymous class.Key points about functional interfaces:
Example:
@FunctionalInterface
public interface MyFunctionalInterface {
void execute();
}
// Using a lambda expression to implement the functional interface
MyFunctionalInterface implementation = () -> System.out.println("Executing...");
implementation.execute(); // Output: Executing...
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 your own functional interface in Java, and the @FunctionalInterface annotation is often used to mark an interface as a functional interface.What is @FunctionalInterface?
Rules to Define a Functional Interface
Example of a Custom Functional Interface
@FunctionalInterface
interface MyFunctionalInterface {
// Single abstract method
void execute();
// Default method
default void defaultMethod() {
System.out.println("Default method");
}
// Static method
static void staticMethod() {
System.out.println("Static method");
}
}
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 introduced in the java.util.function package and the more common ones include but are not limited to:- Function<T, R>: Represents a function that takes an argument of type T and returns a result of type R.
- Consumer<T>: Represents an operation that takes a single input argument of type T and returns no result (performs a side effect).
- Supplier<T>: Represents a supplier of results, taking no arguments but providing a result of type T.
- Predicate<T>: Represents a boolean-valued function of one argument of type T. Commonly used for filtering or matching.
- UnaryOperator<T>: Represents a function that takes a single argument of type T and returns a result of the same type. It extends Function<T, T>.
- BinaryOperator<T>: Represents a function that takes two arguments of type T and returns a result of the same type. It extends BiFunction<T, T, T>.
- BiFunction<T, U, R>: Represents a function that takes two arguments of types T and U and returns a result of type R.
- BiConsumer<T, U>: Represents an operation that takes two input arguments of types T and U and returns no result.
- BiPredicate<T, U>: Represents a boolean-valued function that takes two arguments of types T and U.
- ToIntFunction<T>, ToLongFunction<T>, and ToDoubleFunction<T>: Represents functions that take an argument of type T and return a primitive int, long, or double value, respectively.
- IntFunction<R>, LongFunction<R>, DoubleFunction<R>: Represents functions that take a primitive int, long, or double value and return a result of type R.
4. What Is the Difference Between a Normal and Functional Interface in Java?
In Java, the distinction between a normal interface and a functional interface is mainly related to the number of abstract methods they contain and their intended usage.Normal Interface:
Functional Interface:
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 of different types.
@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"));
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.@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);
}
Example:
Predicate < Integer > predicate = (t) -> { if (t % 2 == 0) { return true; } else { return false; } }; System.out.println(predicate.test(10));
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");
Ramesh
Read more at Java 8 Consumer Interface Example
8. What is the Supplier interface?
@FunctionalInterface
public interface Supplier<T> {
T get();
}
Supplier < LocalDateTime > supplier = () -> LocalDateTime.now(); System.out.println(supplier.get());
2020-04-30T11:32:51.628
9. What is 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));
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
Comments
Post a Comment
Leave Comment