Monday, 13 May 2019

Java 8 BiConsumer, BiFunction, and BiPredicate Interfaces with Examples

         In earlier posts, we discussed the Java 8 Consumer interface, method references, and the forEach() method with examples. In this post, we will discuss the BiConsumer, BiFunction, and BiPredicate interfaces with examples.

These functional interfaces accept two input parameters. BiConsumer performs an operation without returning a result, BiFunction accepts two inputs and returns a result, and BiPredicate accepts two inputs and returns a boolean value. We will discuss each interface in detail below.


  • BiConsumer (java.util.function package)

         BiConsumer is a functional interface that contains a single abstract method, accept(), similar to the Consumer interface. The main difference is that the Consumer interface accepts a single input parameter, whereas the BiConsumer interface accepts two input parameters. Neither interface returns a result.

Example:-

package com.test;

import java.util.HashMap;
import java.util.Map;
import java.util.function.BiConsumer;
import java.util.function.Consumer;

public class BiConsumerExample {
 
         public static void main(String[] args) {
      
                   //Consumer example
                  Consumer<String> strConsumer = str -> System.out.println(str);
                  strConsumer.accept("Java Wolrd");
  
                  //BiConsumer example
                  Map<String, String> map = new HashMap<String, String>();
                  map.put("Hello", "World");
                  BiConsumer<String, String> consumer = (key, value) -> System.out.println("Key - "+ key+", Value - "+value);
    
                  map.forEach(consumer);
         }

}

Output:-
Java World
Key - Hello, Value - World
  • BiFunction (java.util.function package)
         BiFunction interface is also similar to Function interface(It is also functional interface), difference is Function interface will accept one parameter but BiFunction interface accepts two parameters as a input and returns results.

Declaration:-

Interface BiFunction<T,U,R>

Here, 

T - the type of the first argument as input
U - the type of the second argument as input
R - the type of the result of the function

Example:-

package com.test;

import java.util.function.BiFunction;
import java.util.function.Function;

public class BiFunctionExample {
 
         public static void main(String[] args) {
  
                 //Function example
                Function<Integer, Integer> printNumber = a -> a*10;
                System.out.println("Number is "+printNumber.apply(10));
  
                 //BiFunction example
                BiFunction<Integer, Integer, Integer> add = (a, b) -> a+b;
                System.out.println("Summation of two number is "+add.apply(3,2));
        }

}

Output:-
Number is 100
Summation of two number is 5
  • BiPredicate (java.util.function package)
        This interface also similar to Predicate interface and it's also functional interface. Predicate interface takes one agrument as input and return boolean value. But BiPredicate interface takes two arguments as input and returns boolean data type same as Predicate interface.

Example:-

package com.test;

import java.util.function.BiPredicate;
import java.util.function.Predicate;

public class BiPredicateExample {
 
         public static void main(String[] args) {
                
                 //Predicate example
                Predicate<String> pr = a -> a.contains("A");
                System.out.println(pr.test("Anil"));
  
                 //BiPredicate example
                BiPredicate<Integer, Integer> biPrdt = (a,b) -> a>10 && b<5;
                System.out.println(biPrdt.test(11, 2));
         }
}

Output:-
true
true


Thank you for visiting the blog.

No comments:

Post a Comment