🎓 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
HashMap.replaceAll() method in Java is used to replace each entry's value in the HashMap with the result of applying a given function to that entry. This guide will cover the method's usage, explain how it works, and provide examples to demonstrate its functionality.Table of Contents
- Introduction
replaceAllMethod Syntax- Examples
- Replacing All Values in a HashMap
- Real-World Use Case: Adjusting Employee Salaries
- Conclusion
Introduction
The HashMap.replaceAll() method is a member of the HashMap class in Java. It allows you to replace the value for each entry in the HashMap using a specified function. This can be useful for applying a consistent transformation to all values in the map.
replaceAll() Method Syntax
The syntax for the replaceAll method is as follows:
public void replaceAll(BiFunction<? super K, ? super V, ? extends V> function)
- The method takes a single parameter:
functionof typeBiFunction<? super K, ? super V, ? extends V>, which represents the function to compute a new value for each entry.
- The method does not return any value.
Examples
Replacing All Values in a HashMap
The replaceAll method can be used to replace all values in a HashMap based on a specified function.
Example with Lambda Expression
import java.util.HashMap;
import java.util.function.BiFunction;
public class ReplaceAllExample {
public static void main(String[] args) {
// Creating a HashMap with String keys and Integer values
HashMap<String, Integer> people = new HashMap<>();
// Adding entries to the HashMap
people.put("Ravi", 25);
people.put("Priya", 30);
people.put("Vijay", 35);
// Using replaceAll method to increase each age by 5
people.replaceAll((key, value) -> value + 5);
// Printing the updated HashMap
System.out.println("Updated HashMap: " + people);
}
}
Output:
Updated HashMap: {Ravi=30, Priya=35, Vijay=40}
Real-World Use Case: Adjusting Employee Salaries
In a real-world scenario, you might use the replaceAll method to adjust employee salaries by applying a percentage increase.
Example with Lambda Expression
import java.util.HashMap;
public class AdjustEmployeeSalaries {
public static void main(String[] args) {
// Creating a HashMap with String keys (employee IDs) and Integer values (salaries)
HashMap<String, Integer> employeeSalaries = new HashMap<>();
// Adding entries to the HashMap
employeeSalaries.put("E001", 50000);
employeeSalaries.put("E002", 60000);
employeeSalaries.put("E003", 70000);
// Using replaceAll method to apply a 10% salary increase
employeeSalaries.replaceAll((key, value) -> value + (value / 10));
// Printing the updated employee salaries
System.out.println("Updated Employee Salaries: " + employeeSalaries);
}
}
Output:
Updated Employee Salaries: {E001=55000, E002=66000, E003=77000}
Conclusion
The HashMap.replaceAll() method in Java provides a way to replace each entry's value in the HashMap with the result of applying a given function to that entry. By understanding how to use this method, you can efficiently transform all values in your map according to a specified rule. This method is useful in various scenarios, such as updating values based on a calculation, applying consistent changes, and managing data transformations. Using lambda expressions with this method makes the code more concise and readable.
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