BiConsumer is a functional interface that takes two arguments and has no return value.
@FunctionalInterface
public interface BiConsumer<T, U> {
/**
* Performs this operation on the given arguments.
*
* @param t the first input argument
* @param u the second input argument
*/
void accept(T t, U u);
}
1. BiConsumer
BiConsumer can be implemented as a Lambda expression.
If you pass two arguments with the accept()
call, the implementation is executed.
import java.util.function.BiConsumer;
public class BiConsumerExample1 {
public static void main(String[] args) {
BiConsumer<Integer, Integer> biConsumer = (n1, n2) -> System.out.println(n1 * n2);
biConsumer.accept(10, 20);
}
}
Output:
200
2. High Order Function
BiConsumer can be passed as an argument to a function.
import java.util.function.BiConsumer;
public class BiConsumerExample2 {
public static void main(String[] args) {
BiConsumer<Integer, Integer> biConsumer = (n1, n2) -> System.out.println(n1 * n2);
multiply(10, 20, biConsumer);
}
static void multiply(Integer n1, Integer n2, BiConsumer<Integer, Integer> biConsumer) {
biConsumer.accept(n1, n2);
}
}
Output:
200
3. Map.forEach()
Map.forEach()
takes a BiConsumer as an argument.
You can output all the contents of a Map by passing a BiConsumer like this:
import java.util.HashMap;
import java.util.Map;
import java.util.function.BiConsumer;
public class BiConsumerExample3 {
public static void main(String[] args) {
Map<String, Integer> map = new HashMap<>();
map.put("one", 1);
map.put("two", 2);
map.put("three", 3);
BiConsumer<String, Integer> biConsumer =
(key, value) -> System.out.println("key: " + key + ", value: " + value);
map.forEach(biConsumer);
}
}
Output:
key: one, value: 1
key: two, value: 2
key: three, value: 3
4. andThen()
andThen()
is a chaining method that connects BiConsumers.
When calling biConsumer1.andThen(biConsumer2).accept(10, 20)
in the code below, biConsumer1
is executed first, followed by biConsumer2
.
Two BiConsumers are passed 10 and 20 as arguments.
import java.util.function.BiConsumer;
public class BiConsumerExample4 {
public static void main(String[] args) {
BiConsumer<Integer, Integer> biConsumer1 =
(n1, n2) -> System.out.println("multiply: " + (n1 * n2));
BiConsumer<Integer, Integer> biConsumer2 =
(n1, n2) -> System.out.println("sum: " + (n1 + n2));
biConsumer1.andThen(biConsumer2).accept(10, 20);
}
}
Output:
multiply: 200
sum: 30
In BiConsumer.java
, andThen()
is implemented as follows.
// BiConsumer.java
default BiConsumer<T, U> andThen(BiConsumer<? super T, ? super U> after) {
Objects.requireNonNull(after);
return (l, r) -> {
accept(l, r);
after.accept(l, r);
};
}
References
Related Posts
- Java - Remove items from List while iterating
- Java - How to find key by value in HashMap
- Java - Update the value of a key in HashMap
- Java - How to put quotes in a string
- Java - How to put a comma (,) after every 3 digits
- BiConsumer example in Java 8
- Java 8 - Consumer example
- Java 8 - BinaryOperator example
- Java 8 - BiPredicate Example
- Java 8 - Predicate example
- Java 8 - Convert Stream to List
- Java 8 - BiFunction example
- Java 8 - Function example
- Java - Convert List to Map
- Exception testing in JUnit
- Hamcrest Collections Matcher
- Hamcrest equalTo () Matcher
- AAA pattern of unit test (Arrange/Act/Assert)
- Hamcrest Text Matcher
- Hamcrest Custom Matcher
- Why Junit uses Hamcrest
- Java - ForkJoinPool
- Java - How to use Futures
- Java - Simple HashTable implementation
- Java - Create a file in a specific path
- Java - Mockito의 @Mock, @Spy, @Captor, @InjectMocks
- Java - How to write test code using Mockito
- Java - Synchronized block
- Java - How to decompile a ".class" file into a Java file (jd-cli decompiler)
- Java - How to generate a random number
- Java - Calculate powers, Math.pow()
- Java - Calculate the square root, Math.sqrt()
- Java - How to compare String (==, equals, compare)
- Java - Calculate String Length
- Java - case conversion & comparison insensitive (toUpperCase, toLowerCase, equalsIgnoreCase)