Monday 13 May 2019

Java 8 - BiConsumer, BiFunction and BiPredicate Interface Example

        We discussed in earlier posts, Java 8 Consumer Interface, method reference and forEach() method with examples. In this page or post, we will discuss about BiConsumer, BiFunction and BiPredicate Interface with examples.

       All these Interfaces accept two input parameters and returns a result. We will discuss more details of each interface as below.

  • BiConsumer (java.util.function package)

        BiConsumer is a functional interface, has one abstract method i.e accept(), similar to a Consumer Interface. The difference between consumer and BiConsumer is Consumer interface accepts single parameter where as BiConsumer accepts two input parameters, and both doesn’t return anything.


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);


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.


Interface BiFunction<T,U,R>


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


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));


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.


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");
                 //BiPredicate example
                BiPredicate<Integer, Integer> biPrdt = (a,b) -> a>10 && b<5;
                System.out.println(biPrdt.test(11, 2));


Related Posts:-
1) Java 8 forEach method with examples
2) Method References in Java 8
3) Java 8 features with examples
4) Java 8 - Consumer Interface with examples

No comments:

Post a Comment