October 30, 2022

BiConsumer Functional Interface Java Examples

In this post we’ll see examples of Java BiConsumer functional interface.

BiConsumer functional interface represents an operation that accepts two arguments and returns no result. Abstract method in this functional interface is accept(T t, U u) and there is also one default method andThen(BiConsumer<? super T,? super U> after).

If you are writing a Lambda expression that needs two arguments and doesn’t return a value then that lambda expression can be written as an implementation of BiConsumer built-in functional interface.

BiConsumer functional interface example

1. In the example a Map is created and then entries of the Map are displayed. We’ll use lambda expression that implements the BiConsumer functional interface. Lambda expression you write implements the abstract method of the functional interface so in the case of BiConsumer functional interface, lambda expression is implementing the accept() method.

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

public class BiConsumerExample {
  public static void main(String[] args) {
    // Implementation
    BiConsumer<String, String> biCons = (K, V) -> 
          System.out.println("Key is- " +K + " Value is- " + V);
    Map<String, String> numMap = new HashMap<String, String>();
    numMap.put("1", "One");
    numMap.put("2", "Two");
    numMap.put("3", "Three");
    numMap.put("4", "Four");
    for(Map.Entry<String, String> entry : numMap.entrySet()) {
      // calling accept method
      biCons.accept(entry.getKey(), entry.getValue());
    }
  }
}
Output
Key is- 1 Value is- One
Key is- 2 Value is- Two
Key is- 3 Value is- Three
Key is- 4 Value is- Four

In the program, statement BiConsumer<String, String> biCons = (K, V) -> System.out.println("Key is- " +K + " Value is- " + V); is the implementation of BiConsumer as a lambda expression. Since it is an instance of a functional interface so assigned to variable of type BiConsumer.

Lambda supports "target typing" which infers the object type from the context in which it is used. When biCons.accept() method is called Java can infer from the context where to look for the implementation of accept() method.

2. Here is another example where add() and multiply() methods are implemented using BiConsumer interface.

public class BiConsumerExample {
  public static void main(String[] args) {
    // Implementation as add method
    BiConsumer<Integer, Integer> biConsAdd = (a, b) -> 
          System.out.println("Sum is- " + (a + b));
    // Implementation as multiplication method
    BiConsumer<Integer, Integer> biConsMul = (a, b) -> 
          System.out.println("Multiplication is- " + (a * b));

    biConsAdd.accept(5, 6);
    biConsMul.accept(5, 6);
  }
}
Output
Sum is- 11
Multiplication is- 30

BiConsumer functional interface andThen() method example

There is also a default method andThen() in BiConsumer interface.

BiConsumer<T,U> andThen(BiConsumer<? super T,? super U> after)- Takes another BiConsumer as argument and returns a composed BiConsumer that performs, in sequence, first the operation of the calling BiConsumer followed by the after operation.

In the previous example addition and multiplication are called in sequence for the same set of arguments. If that is the case then it can use the andThen() method.

public class BiConsumerExample {
  public static void main(String[] args) {
    // Implementation as add method
    BiConsumer<Integer, Integer> biConsAdd = (a, b) -> 
          System.out.println("Sum is- " + (a + b));
    // Implementation as multiplication method
    BiConsumer<Integer, Integer> biConsMul = (a, b) -> 
          System.out.println("Multiplication is- " + (a * b));
    //First add then multiply in sequence			
    biConsAdd.andThen(biConsMul).accept(5, 6);

    //biConsAdd.accept(5, 6);
    //biConsMul.accept(5, 6);
  }
}
Output
Sum is- 11
Multiplication is- 30

BiConsumer functional interface in JDK

These built-in functional interfaces are used extensively with in the JDK itself. One example of BiConsumer functional interface used quite often is when forEach() method is used to iterate a Map. In the Map interface there is a forEach() method that takes BiConsumer as an argument.

forEach(BiConsumer<? super K,? super V> action)- Performs the given action for each entry in this map until all entries have been processed or the action throws an exception.

First example where we iterated a Map can be written more concisely using forEach().

public class BiConsumerExample {
  public static void main(String[] args) {
    Map<String, String> numMap = new HashMap<String, String>();
    numMap.put("1", "One");
    numMap.put("2", "Two");
    numMap.put("3", "Three");
    numMap.put("4", "Four");
    numMap.forEach( (K, V) -> System.out.println("Key is- " + K + " Value is- " + V));
  }
}
Output
Key is- 1 Value is- One
Key is- 2 Value is- Two
Key is- 3 Value is- Three
Key is- 4 Value is- Four

That's all for the topic BiConsumer Functional Interface Java Examples. If something is missing or you have something to share about the topic please write a comment.


You may also like

No comments:

Post a Comment