Bi-Functional Interfaces

Need of Two-Argument (Bi) Functional Interfaces

Normal Functional Interfaces (Predicate, Function and Consumer) can accept only one input argument. But sometimes our programming requirement is to accept two input arguments, then we should go for two-argument (Bi) functional interfaces. The following functional interfaces can take 2 input arguments.

1. BiPredicate
2. BiFunction
3. BiConsumer

1. BiPredicate

Normal Predicate can take only one input argument and perform some conditional check. Sometimes our programming requirement is we have to take 2 input arguments and perform some conditional check, for this requirement we should go for BiPredicate. BiPredicate is exactly same as Predicate except that it will take 2 input arguments.

interface BiPredicate<T1,T2> {
   public boolean test(T1 t1,T2 t2);
   //remaining default methods: and(), or() , negate()
}

Check the sum of 2 given integers is even or not

package com.ashok.java8;

import java.util.function.BiPredicate;

/**
 * 
 * @author ashok.mariyala
 *
 */
public class BiPredicateTest {
   public static void main(String[] args) {
      BiPredicate<Integer, Integer> p = (a, b) -> (a + b) % 2 == 0;
      System.out.println(p.test(25,30)); // false
      System.out.println(p.test(50,60)); // true
   }
}
2. BiFunction

Normal Function can take only one input argument and perform required operation and returns the result. The result need not be boolean type. But sometimes our programming requirement to accept 2 input values and perform required operation and should return the result. Then we should go for BiFunction. BiFunction is exactly same as function except that it will take 2 input arguments.

interface BiFunction<T,U,R> {
   public R apply(T t,U u);
   //default method andThen()
}

Find product of 2 given integers

package com.ashok.java8;

import java.util.function.BiFunction;

/**
 * 
 * @author ashok.mariyala
 *
 */
public class BiFunctionTest {
   public static void main(String[] args) {
      BiFunction<Integer, Integer, Integer> f = (a, b) -> a * b;
      System.out.println(f.apply(10, 15)); // 150
      System.out.println(f.apply(25, 20)); // 500
   }
}
3. BiConsumer

Normal Consumer can take only one input argument and perform required operation and won’t return any result. But sometimes our programming requirement to accept 2 input values and perform required operation and not required to return any result. Then we should go for BiConsumer. BiConsumer is exactly same as Consumer except that it will take 2 input arguments.

interface BiConsumer<T,U> { 
   public void accept(T t,U u); 
   //default method andThen()
}

Accept 2 String values and print result

package com.ashok.java8;

import java.util.function.BiConsumer;

/**
 * 
 * @author ashok.mariyala
 *
 */
public class BiConsumerTest {
   public static void main(String[] args) {
      BiConsumer<String, String> c = (s1, s2) -> System.out.println(s1 + s2);
      c.accept("Ashok ","Kumar"); // Ashok Kumar
   }
}

Comparison Table between One argument and Two argument Functional Interfaces

Capture 48
Bi-Functional Interfaces
Scroll to top