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.biconsumer.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)
Example: Using Map.forEach(BiConsumer) method:
BiConsumerEx2.java
package org.o7planning.biconsumer.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 + Method reference

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

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

public class BiConsumer_mRef_ex1 {

    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");
    
       // Static method reference:
       BiConsumer<String, String> bc = MyUtils::printContactInfo;
           
       contacts.forEach(bc);
    }  
}

class MyUtils {
    // Static method:
    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
Example: BiConsumer + Non-static method reference:
BiConsumer_mRef_ex2.java
package org.o7planning.biconsumer.ex;

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

public class BiConsumer_mRef_ex2 {

    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");
       
       CardTemplate template = new CardTemplate("Designed by Tom");
    
       // Non-static method reference:
       BiConsumer<String, String> bc = template::printCard;
           
       contacts.forEach(bc);
    }  
}

class CardTemplate {
    private String someInfo;
    
    public CardTemplate(String someInfo) {
        this.someInfo = someInfo;
    }
    
    // Non-static Method:
    public void printCard(String phone, String name)  {
        System.out.println("--- ~~~ ---");
        System.out.println("Full Name: " + name);
        System.out.println("Phone: " + phone);
        System.out.println(someInfo);
        System.out.println();
    }
}
Output:
--- ~~~ ---
Full Name: David Garcia
Phone: 1233 5555
Designed by Tom

--- ~~~ ---
Full Name: James Smith
Phone: 1111 2222
Designed by Tom

--- ~~~ ---
Full Name: Michael Smith
Phone: 1111 3333
Designed by Tom

3- andThen(BiConsumer after)

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:
BiConsumer_andThen_ex1.java
package org.o7planning.biconsumer.ex;

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

public class BiConsumer_andThen_ex1 {

    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

4- Example: Map to Map

Example: A Map<String,Integer> object contains mappings between employee number and salary. Create a similar Map<String,Integer> object with double salary.
BiConsumer_m2m_ex1.java
package org.o7planning.biconsumer.ex;

import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;

public class BiConsumer_m2m_ex1 {

    public static void main(String[] args) {
        // String employeeNumber --> Integer salary.
        Map<String, Integer> empSalaryMap = new HashMap<String, Integer>();

        empSalaryMap.put("E01", 1000);
        empSalaryMap.put("E02", 1500);
        empSalaryMap.put("E03", 3000);

        Map<String, Integer> newEmpSalaryMap //
        
                = empSalaryMap.entrySet() // Set<Map.Entry<String,Integer>>
                        .stream() // // Stream<Map.Entry<String,Integer>>
                        // Call method:
                        // collect(Supplier<R>, BiConsumer<R, ? extends Map.Entry<String,Integer>>)
                        .collect(Collectors.toMap(entry -> entry.getKey(), entry -> 2 * entry.getValue()));

        System.out.println("Origin Map: " + empSalaryMap);
        System.out.println();
        System.out.println("New Map: " + newEmpSalaryMap);
    }
}
Output:
Origin Map: {E02=1500, E01=1000, E03=3000}

New Map: {E02=3000, E01=2000, E03=6000}
Basically, you can process one Map<K,V> object to form another Map<K2,V2>. See more articles below:

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.