🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
Introduction
In Java, the BiConsumer is a functional interface that represents an operation that takes two input arguments and returns no result. It's useful for operations involving two parameters, like updating data structures or logging.
Table of Contents
- What is
BiConsumer? - Methods and Syntax
- Examples of
BiConsumer - Real-World Use Case
- Conclusion
1. What is BiConsumer?
BiConsumer is a functional interface that takes two arguments and operates on them without returning a result. It is commonly used in lambda expressions and method references.
2. Methods and Syntax
The main method in the BiConsumer interface is:
void accept(T t, U u): Performs this operation on the given arguments.
Syntax
BiConsumer<T, U> biConsumer = (T t, U u) -> {
// operation on t and u
};
3. Examples of BiConsumer
Example 1: Printing Two Values
import java.util.function.BiConsumer;
public class BiConsumerExample {
public static void main(String[] args) {
// Define a BiConsumer that prints a name and age
BiConsumer<String, Integer> print = (name, age) -> {
System.out.println(name + " is " + age + " years old.");
};
print.accept("Raj", 30);
}
}
Output:
Raj is 30 years old.
Example 2: Updating a Map
import java.util.HashMap;
import java.util.Map;
import java.util.function.BiConsumer;
public class BiConsumerMapExample {
public static void main(String[] args) {
// Create a map with fruit names and their counts
Map<String, Integer> map = new HashMap<>();
map.put("Apple", 3);
map.put("Banana", 2);
// Define a BiConsumer to update the map values
BiConsumer<String, Integer> updateMap = (key, value) -> map.put(key, value + 1);
map.forEach(updateMap);
System.out.println(map);
}
}
Output:
{Apple=4, Banana=3}
4. Real-World Use Case: Inventory Management
In inventory management systems, BiConsumer can be used to update stock levels based on incoming shipments.
import java.util.HashMap;
import java.util.Map;
import java.util.function.BiConsumer;
public class InventoryManagement {
public static void main(String[] args) {
// Inventory map with item names and quantities
Map<String, Integer> inventory = new HashMap<>();
inventory.put("Laptops", 50);
inventory.put("Monitors", 30);
// Shipments map with items and quantities to add
Map<String, Integer> shipments = new HashMap<>();
shipments.put("Laptops", 10);
shipments.put("Monitors", 5);
// BiConsumer to update inventory based on shipments
BiConsumer<String, Integer> updateInventory = (item, quantity) ->
inventory.merge(item, quantity, Integer::sum);
// Process each shipment
shipments.forEach(updateInventory);
System.out.println(inventory);
}
}
Output:
{Laptops=60, Monitors=35}
Conclusion
The BiConsumer interface is a versatile tool in Java for performing operations on two inputs without returning a result. It simplifies handling operations in functional programming and is especially useful in data processing and inventory management scenarios.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment