Which functional interface in Java takes two arguments and returns a result?

Which functional interface in Java takes two arguments and returns a result?

a) Function
b) BiFunction
c) Consumer
d) Predicate

Answer:

b) BiFunction

Explanation:

The BiFunction functional interface in Java is used to represent a function that takes two arguments and returns a result. It is similar to the Function interface but operates on two inputs instead of one. Here is an example:

BiFunction<Integer, Integer, Integer> add = (a, b) -> a + b; int result = add.apply(5, 10);  // result is 15

In this example, the lambda expression adds two integers and returns the result. The BiFunction interface is particularly useful when working with operations that require two inputs, such as combining or comparing values.

Using the BiFunction interface allows developers to create more flexible and reusable functions that can operate on pairs of data. This is especially useful in scenarios such as performing mathematical operations, combining strings, or processing data from two different sources.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top