o7planning

Java BiConsumer Tutorial

View more Tutorials:

1- BiConsumer

In Java 8, BiConsumer is a functional interface, representing an operator that accepts two input parameters and returns nothing.
BiConsumer
@FunctionalInterface
public interface BiConsumer<T, U> {
  void accept(T t, U u);

  default BiConsumer<T, U> andThen(BiConsumer<? super T, ? super U> after);
}
See more: Consumer is an interface similar to BiConsumer, but it only accepts one parameter: 
Example:
BiConsumerEx1.java
package org.o7planning.ex;

import java.util.function.BiConsumer;

public class BiConsumerEx1 {

    public static void main(String[] args) {
        
        // Create a BiConsumer object directly
        BiConsumer<String, String> greeter
             = (firstName, lastName) -> System.out.println("Hello " + firstName+ " " + lastName);
         
        greeter.accept("James", "Smith");
    }
}
Output:
Hello James Smith
Below is a list of methods in java.util package using BiConsumer:
Modifier and Type Method and Description
void IdentityHashMap.forEach(BiConsumer<? super K,? super V> action) 
void TreeMap.forEach(BiConsumer<? super K,? super V> action) 
void LinkedHashMap.forEach(BiConsumer<? super K,? super V> action) 
void Hashtable.forEach(BiConsumer<? super K,? super V> action) 
void HashMap.forEach(BiConsumer<? super K,? super V> action) 
void WeakHashMap.forEach(BiConsumer<? super K,? super V> action) 
default void Map.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.
Example: Using Map.forEach(BiConsumer) method:
BiConsumerEx2.java
package org.o7planning.ex;

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

public class BiConsumerEx2 {

    public static void main(String[] args) {
         
        BiConsumer<String, String> printer
             = (phoneNumber, fullName) -> System.out.println(phoneNumber + " - " + fullName);
       
       Map<String, String> contacts = new HashMap<String,String>();
       contacts.put("1111 2222", "James Smith");
       contacts.put("1111 3333", "Michael Smith");
       contacts.put("1233 5555", "David Garcia");
       
       contacts.forEach(printer);
    }  
}
Output:
1233 5555 - David Garcia
1111 2222 - James Smith
1111 3333 - Michael Smith

2- BiConsumer.andThen(..)

andThen(after) method returns an associated BiConsumer. First, the current BiConsumer is called then after will be called next . If an error occurs in one of the two steps above, the error is passed to caller. If an error occurs at the current BiConsumer then after is ignored.
andThen method
default BiConsumer<T, U> andThen(BiConsumer<? super T, ? super U> after) {
    Objects.requireNonNull(after);

    return (l, r) -> {
        accept(l, r);
        after.accept(l, r);
    };
}
Example:
BiConsumerEx3.java
package org.o7planning.ex;

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

public class BiConsumerEx3 {

    public static void main(String[] args) {  
       Map<String, String> contacts = new HashMap<String,String>();
       contacts.put("1111 2222", "James Smith");
       contacts.put("1111 3333", "Michael Smith");
       contacts.put("1233 5555", "David Garcia");
       
       BiConsumer<String, String> printer1
           = (phoneNumber, fullName) -> System.out.println(phoneNumber + " - " + fullName);
           
       BiConsumer<String, String> printer2
           = (phoneNumber, fullName) -> System.out.println(phoneNumber + " - " + fullName.toUpperCase());  
           
       contacts.forEach(printer1.andThen(printer2));
    }  
}
Output:
1233 5555 - David Garcia
1233 5555 - DAVID GARCIA
1111 2222 - James Smith
1111 2222 - JAMES SMITH
1111 3333 - Michael Smith
1111 3333 - MICHAEL SMITH

3- BiConsumer + Method reference

If a method takes two parameters and returns nothing, then its reference can be considered as a BiConsumer.
Example:
BiConsumerEx4.java
package org.o7planning.ex;

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

public class BiConsumerEx4 {

    public static void main(String[] args) {  
       Map<String, String> contacts = new HashMap<String,String>();
       contacts.put("1111 2222", "James Smith");
       contacts.put("1111 3333", "Michael Smith");
       contacts.put("1233 5555", "David Garcia");
    
       // Method reference:
       BiConsumer<String, String> bc = MyUtils::printContactInfo;
           
       contacts.forEach(bc);
    }
    
    public static class MyUtils {
        
        public static void printContactInfo(String phone, String name)  {
            System.out.println("Phone: " + phone + " / Full Name: " + name);
        }
    }
}
Output:
Phone: 1233 5555 / Full Name: David Garcia
Phone: 1111 2222 / Full Name: James Smith
Phone: 1111 3333 / Full Name: Michael Smith

View more Tutorials:

Maybe you are interested

These are online courses outside the o7planning website that we introduced, which may include free or discounted courses.