Predefined Functional interfaces with 2 input arguments

  • Last Updated: August 18, 2023
  • By: javahandson
  • Series
img

Predefined Functional interfaces with 2 input arguments

In this article, we will learn about predefined functional interfaces with 2 input arguments. We will also learn the most commonly used 2 input arguments functional interfaces with proper examples.

In the previous chapter we have seen predefined functional interfaces like Predicate, Function and Consumer take only one input argument. It will perform some logic on that input argument and returns a value.

But sometimes our requirement can be to perform an operation on 2 input arguments. So which functional interfaces should we use? Below are the predefined functional interfaces that take 2 input arguments.

BiPredicate interface

Note* Bi means 2

BiPredicate functional interface is used to perform the conditional check. It is having an abstract method named test. This test method will accept 2 input arguments of Type T and U perform a conditional check and returns a boolean.

BiPredicate is exactly the same as the Predicate interface except it will take 2 input arguments.

@FunctionalInterface
public interface BiPredicate<T, U> {
   boolean test(T t, U u);  
}

Parameters:

t is an input argument of type T ( means the predicate interface can work with any type )
u is an input argument of type U ( means the predicate interface can work with any type )

Returns: boolean ( returns true if the condition matches else false )

Write a program to check if the sum of 2 numbers is an even number.

package com.javahandson.predefined;
import java.util.function.BiPredicate;

public class Test {
    public static void main(String[] args) {

        BiPredicate<Integer, Integer> biPredicate = (t, u) -> (t+u) % 2 == 0;

        boolean result = biPredicate.test(10, 12);
        System.out.println("Sum of input arguments 10 and 12 is an even number : "+result);

        result = biPredicate.test(10, 11);
        System.out.println("Sum of input arguments 10 and 11 is an even number : "+result);
    }
}
Output : Sum of input arguments 10 and 12 is an even number : true
         Sum of input arguments 10 and 11 is an even number : false

The above BiPredicate function accepts 2 arguments of Integer type executes logic and returns a boolean.

BiFunction Interface

BiFunction functional interface is having an abstract method named apply. The apply method will accept 2 input arguments of Type T and U perform some operation and returns a value of Type R.

BiFunction is exactly the same as the Function interface except it will take 2 input arguments.

@FunctionalInterface
public interface BiFunction<T,U,R> {
      R apply(T t, U u);  // abstract method
}

Parameters:

t is an input argument of type T ( means the predicate interface can work with any type )
u is an input argument of type U ( means the predicate interface can work with any type )

Returns: R ( R can be of any type )

Write a program to calculate and return the multiplication of 2 numbers.

package com.javahandson.predefined;
import java.util.function.BiFunction;

public class Test {
    public static void main(String[] args) {

        BiFunction<Integer, Integer, Integer> biFunction = (t, u) -> t * u;
        int result = biFunction.apply(10, 12);
        System.out.println("Multiplication of 2 numbers 10 and 12 is : "+result);
    }
}
Output : Multiplication of 2 numbers 10 and 12 is : 120

The above BiFunction function accepts 2 arguments of Integer type executes logic and returns an Integer.

BiConsumer interface

The BiConsumer interface is having an abstract method named accept. This accept method will accept 2 input arguments of Type T and U performs some operation but it will not return any value. The return type is void.

BiConsumer is exactly the same as the Consumer interface except it will take 2 input arguments.

@FunctionalInterface
public interface BiConsumer<T,U> {
    void accept(T t, U u);
}

Parameters:

t is an input argument of type T ( means the predicate interface can work with any type )
u is an input argument of type U ( means the predicate interface can work with any type )

Returns: void ( nothing )

Write a program to calculate the multiplication of 2 numbers and print it.

package com.javahandson.predefined;
import java.util.function.BiConsumer;

public class Test {
    public static void main(String[] args) {

        BiConsumer<Integer, Integer> biConsumer = (t, u) -> {
             System.out.println("Multiplication value is : "+t*u);
        };
        biConsumer.accept(10, 12);
    }
}
Output : Multiplication value is : 120

The above BiConsumer accepts 2 arguments of Integer type executes logic and prints the value. It does not return anything.

BiSupplier interface

There is no such interface in Java.

The Bi term means 2 input arguments. But as we know Supplier interface takes no argument and returns a value. Hence if there was a BiSupplier then it should not have taken any arguments. But the Bi term would have made no sense because we require 2 input arguments when we say Bi. Hence BiSupplier interface doesn’t exist.

So this is all about predefined functional interfaces with 2 input arguments in Java 8. I hope you like the article. If you are having any questions on this topic please raise them in the comments section.

Leave a Comment