Java BiConsumer Tutorial with Examples
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:
- Java Stream.collect method
Java Basic
- Customize java compiler processing your Annotation (Annotation Processing Tool)
- Java Programming for team using Eclipse and SVN
- Java WeakReference Tutorial with Examples
- Java PhantomReference Tutorial with Examples
- Java Compression and Decompression Tutorial with Examples
- Configuring Eclipse to use the JDK instead of JRE
- Java String.format() and printf() methods
- Syntax and new features in Java 8
- Java Regular Expressions Tutorial with Examples
- Java Multithreading Programming Tutorial with Examples
- JDBC Driver Libraries for different types of database in Java
- Java JDBC Tutorial with Examples
- Get the values of the columns automatically increment when Insert a record using JDBC
- Java Stream Tutorial with Examples
- Java Functional Interface Tutorial with Examples
- Introduction to the Raspberry Pi
- Java Predicate Tutorial with Examples
- Abstract class and Interface in Java
- Access modifiers in Java
- Java Enums Tutorial with Examples
- Java Annotations Tutorial with Examples
- Comparing and Sorting in Java
- Java String, StringBuffer and StringBuilder Tutorial with Examples
- Java Exception Handling Tutorial with Examples
- Java Generics Tutorial with Examples
- Manipulating files and directories in Java
- Java BiPredicate Tutorial with Examples
- Java Consumer Tutorial with Examples
- Java BiConsumer Tutorial with Examples
- What is needed to get started with Java?
- History of Java and the difference between Oracle JDK and OpenJDK
- Install Java on Windows
- Install Java on Ubuntu
- Install OpenJDK on Ubuntu
- Install Eclipse
- Install Eclipse on Ubuntu
- Quick Learning Java for beginners
- History of bits and bytes in computer science
- Data Types in java
- Bitwise Operations
- if else statement in java
- Switch Statement in Java
- Loops in Java
- Arrays in Java
- JDK Javadoc in CHM format
- Inheritance and polymorphism in Java
- Java Function Tutorial with Examples
- Java BiFunction Tutorial with Examples
- Example of Java encoding and decoding using Apache Base64
- Java Reflection Tutorial with Examples
- Java remote method invocation - Java RMI Tutorial with Examples
- Java Socket Programming Tutorial with Examples
- Which Platform Should You Choose for Developing Java Desktop Applications?
- Java Commons IO Tutorial with Examples
- Java Commons Email Tutorial with Examples
- Java Commons Logging Tutorial with Examples
- Understanding Java System.identityHashCode, Object.hashCode and Object.equals
- Java SoftReference Tutorial with Examples
- Java Supplier Tutorial with Examples
- Java Aspect Oriented Programming with AspectJ (AOP)
Show More
- Java Servlet/Jsp Tutorials
- Java Collections Framework Tutorials
- Java API for HTML & XML
- Java IO Tutorials
- Java Date Time Tutorials
- Spring Boot Tutorials
- Maven Tutorials
- Gradle Tutorials
- Java Web Services Tutorials
- Java SWT Tutorials
- JavaFX Tutorials
- Java Oracle ADF Tutorials
- Struts2 Framework Tutorials
- Spring Cloud Tutorials