Java LinkedHashMap replaceAll() Method

The LinkedHashMap.replaceAll(BiFunction<? super K, ? super V, ? extends V> function) method in Java is used to replace each entry's value with the result of applying the given function on the corresponding key and current value.

Table of Contents

  1. Introduction
  2. replaceAll Method Syntax
  3. Examples
    • Modifying All Values
    • Applying Complex Logic
  4. Real-World Use Case
    • Example: Adjusting User Ages
  5. Conclusion

Introduction

The LinkedHashMap.replaceAll(BiFunction<? super K, ? super V, ? extends V> function) method is a member of the LinkedHashMap class in Java. It allows you to replace each value in the map with the result of applying the given function to the key and current value of each entry. This method is useful for bulk operations on all entries 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 one parameter:
    • function of type BiFunction<? super K, ? super V, ? extends V>, which represents the function to be applied to each entry.
  • The method does not return a value.

Examples

Modifying All Values

The replaceAll method can be used to modify all values in a LinkedHashMap.

Example

import java.util.LinkedHashMap;
import java.util.function.BiFunction;

public class ReplaceAllExample {
    public static void main(String[] args) {
        // Creating a LinkedHashMap with String keys and Integer values
        LinkedHashMap<String, Integer> people = new LinkedHashMap<>();

        // Adding entries to the LinkedHashMap
        people.put("Ravi", 25);
        people.put("Priya", 30);
        people.put("Vijay", 35);

        // Using replaceAll to increase each person's age by 1 year
        people.replaceAll(new BiFunction<String, Integer, Integer>() {
            @Override
            public Integer apply(String key, Integer value) {
                return value + 1;
            }
        });

        // Printing the modified LinkedHashMap
        System.out.println("LinkedHashMap after replaceAll: " + people);
    }
}

Output:

LinkedHashMap after replaceAll: {Ravi=26, Priya=31, Vijay=36}

Using Lambda Expression with replaceAll

You can also use a lambda expression to simplify the code.

Example

import java.util.LinkedHashMap;

public class ReplaceAllLambdaExample {
    public static void main(String[] args) {
        // Creating a LinkedHashMap with String keys and Integer values
        LinkedHashMap<String, Integer> people = new LinkedHashMap<>();

        // Adding entries to the LinkedHashMap
        people.put("Ravi", 25);
        people.put("Priya", 30);
        people.put("Vijay", 35);

        // Using replaceAll with a lambda expression to increase each person's age by 1 year
        people.replaceAll((key, value) -> value + 1);

        // Printing the modified LinkedHashMap
        System.out.println("LinkedHashMap after replaceAll: " + people);
    }
}

Output:

LinkedHashMap after replaceAll: {Ravi=26, Priya=31, Vijay=36}

Applying Complex Logic

You can apply more complex logic using the replaceAll method.

Example

import java.util.LinkedHashMap;

public class ComplexLogicExample {
    public static void main(String[] args) {
        // Creating a LinkedHashMap with String keys and Integer values
        LinkedHashMap<String, Integer> people = new LinkedHashMap<>();

        // Adding entries to the LinkedHashMap
        people.put("Ravi", 25);
        people.put("Priya", 30);
        people.put("Vijay", 35);

        // Using replaceAll to apply complex logic: if age is less than 30, add 2 years, else add 1 year
        people.replaceAll((key, value) -> value < 30 ? value + 2 : value + 1);

        // Printing the modified LinkedHashMap
        System.out.println("LinkedHashMap after complex logic: " + people);
    }
}

Output:

LinkedHashMap after complex logic: {Ravi=27, Priya=31, Vijay=36}

Real-World Use Case

Example: Adjusting User Ages

A common real-world use case for LinkedHashMap.replaceAll(BiFunction<? super K, ? super V, ? extends V> function) is adjusting user ages based on a specific rule. For example, let's consider a scenario where we need to add a certain number of years to each user's age based on their current age.

Example

import java.util.LinkedHashMap;

public class AdjustUserAges {
    public static void main(String[] args) {
        // Creating a LinkedHashMap to store user ages
        LinkedHashMap<String, Integer> userAges = new LinkedHashMap<>();

        // Adding user ages to the LinkedHashMap
        userAges.put("Ravi", 25);
        userAges.put("Priya", 30);
        userAges.put("Vijay", 35);

        // Using replaceAll to adjust ages: if age is less than 30, add 3 years, else add 2 years
        userAges.replaceAll((username, age) -> age < 30 ? age + 3 : age + 2);

        // Printing the adjusted user ages
        System.out.println("Adjusted User Ages: " + userAges);
    }
}

Output:

Adjusted User Ages: {Ravi=28, Priya=32, Vijay=37}

In this example, LinkedHashMap.replaceAll(BiFunction<? super K, ? super V, ? extends V> function) is used to adjust user ages based on a specific rule, demonstrating how to apply complex logic to all entries in the map.

Conclusion

The LinkedHashMap.replaceAll(BiFunction<? super K, ? super V, ? extends V> function) method in Java provides a way to replace each value in the map with the result of applying a given function to the key and current value of each entry. By understanding how to use this method, you can efficiently perform bulk operations on all entries in your Java applications, making it a versatile tool for data management.

Comments