The BiConsumer
interface in Java is part of the java.util.function
package and is used when we need to perform an operation that takes two input arguments and does not return any result. It is a functional interface commonly used in lambda expressions or functional programming scenarios.
Key Features:
- It accepts two arguments of potentially different types.
- It does not return a result (void return type).
- It is primarily used for side effect operations (e.g., printing, modifying objects, etc.).
Method in BiConsumer
:
void accept(T t, U u)
: Performs this operation on the given arguments.- Additionally, it has a default method:
default BiConsumer<T, U> andThen(BiConsumer<? super T, ? super U> after)
: Returns a composedBiConsumer
that performs the operation of thisBiConsumer
first, followed by theafter
operation.
Example Usage:
Basic Example with Lambda
package org.kodejava.util.function;
import java.util.function.BiConsumer;
public class BiConsumerExample {
public static void main(String[] args) {
// Create a BiConsumer that adds two numbers and prints the result
BiConsumer<Integer, Integer> addAndPrint =
(a, b) -> System.out.println("Sum: " + (a + b));
// Use the BiConsumer
addAndPrint.accept(10, 20); // Output: Sum: 30
}
}
Using BiConsumer to Manipulate a Map
The BiConsumer
is often used with collections such as Map
.
package org.kodejava.util.function;
import java.util.HashMap;
import java.util.Map;
import java.util.function.BiConsumer;
public class BiConsumerWithMap {
public static void main(String[] args) {
// Map of items
Map<String, Integer> items = new HashMap<>();
items.put("Apples", 10);
items.put("Oranges", 20);
items.put("Bananas", 30);
// Define a BiConsumer to print key-value pairs
BiConsumer<String, Integer> printEntry =
(key, value) -> System.out.println(key + ": " + value);
// Iterate through each entry in the map
items.forEach(printEntry);
}
}
Output:
Apples: 10
Bananas: 30
Oranges: 20
Combining BiConsumers with andThen
The andThen
method allows chaining multiple BiConsumer
operations.
package org.kodejava.util.function;
import java.util.function.BiConsumer;
public class BiConsumerAndThen {
public static void main(String[] args) {
BiConsumer<String, Integer> print =
(key, value) ->
System.out.println("Key: " + key + ", Value: " + value);
BiConsumer<String, Integer> multiplyValue =
(key, value) ->
System.out.println("Multiplied Value for " + key + ": " + (value * 2));
// Combine the two BiConsumers
BiConsumer<String, Integer> combinedBiConsumer = print.andThen(multiplyValue);
// Use the combined BiConsumer
combinedBiConsumer.accept("Apples", 10);
}
}
Output:
Key: Apples, Value: 10
Multiplied Value for Apples: 20
Scenarios to use BiConsumer
:
- Iteration and processing:
- Iterate through a
Map
and perform operations on key-value pairs.
- Iterate through a
- Side effects:
- Logging, printing results, or modifying shared data structures.
- Chaining behaviors:
- Chain operations on a pair of inputs using
andThen
.
- Chain operations on a pair of inputs using
Keynotes:
- Be cautious about side effects as
BiConsumer
is typically used when a return value is not required. - The
andThen
method helps in composing behaviors, making the interface more powerful.