Java HashMap replaceAll() Method

The 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

  1. Introduction
  2. replaceAll Method Syntax
  3. Examples
    • Replacing All Values in a HashMap
    • Real-World Use Case: Adjusting Employee Salaries
  4. 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:
    • function of type BiFunction<? 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.

Comments