Java LinkedHashMap replace(K key, V value) Method

The LinkedHashMap.replace(K key, V value) method in Java is used to replace the value for a specific key if it is currently mapped to some value.

Table of Contents

  1. Introduction
  2. replace Method Syntax
  3. Examples
    • Replacing Existing Values
    • Handling Non-Existent Keys
  4. Real-World Use Case
    • Example: Updating User Information
  5. Conclusion

Introduction

The LinkedHashMap.replace(K key, V value) method is a member of the LinkedHashMap class in Java. It allows you to replace the value associated with a specific key only if it is currently mapped to some value. This method is useful for updating values in a map without needing to check if the key exists separately.

replace() Method Syntax

The syntax for the replace method is as follows:

public V replace(K key, V value)
  • The method takes two parameters:
    • key of type K, which represents the key whose value is to be replaced.
    • value of type V, which represents the new value to be associated with the key.
  • The method returns the previous value associated with the specified key, or null if the key was not found.

Examples

Replacing Existing Values

The replace method can be used to replace existing values in a LinkedHashMap.

Example

import java.util.LinkedHashMap;

public class ReplaceExample {
    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);

        // Replacing value for an existing key
        Integer previousValue = people.replace("Priya", 31);

        // Printing the previous value and the LinkedHashMap after replacement
        System.out.println("Previous value: " + previousValue);
        System.out.println("LinkedHashMap after replacement: " + people);
    }
}

Output:

Previous value: 30
LinkedHashMap after replacement: {Ravi=25, Priya=31, Vijay=35}

Handling Non-Existent Keys

The replace method returns null if the specified key is not found in the LinkedHashMap.

Example

import java.util.LinkedHashMap;

public class NonExistentKeyExample {
    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);

        // Attempting to replace value for a non-existent key
        Integer previousValue = people.replace("Amit", 40);

        // Checking if the key was found and printing the result
        if (previousValue == null) {
            System.out.println("Key 'Amit' does not exist in the LinkedHashMap.");
        } else {
            System.out.println("Previous value: " + previousValue);
        }

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

Output:

Key 'Amit' does not exist in the LinkedHashMap.
LinkedHashMap after attempt: {Ravi=25, Priya=30, Vijay=35}

Real-World Use Case

Example: Updating User Information

A common real-world use case for LinkedHashMap.replace(K key, V value) is updating user information in an application. For example, let's consider a scenario where user profiles are stored in a LinkedHashMap, and we want to update the age of a specific user.

Example

import java.util.LinkedHashMap;

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

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

        // Updating user information
        Integer previousAge = userProfiles.replace("Priya", 31);

        // Printing the previous age and the updated user profiles
        System.out.println("Previous age of Priya: " + previousAge);
        System.out.println("Updated User Profiles: " + userProfiles);
    }
}

Output:

Previous age of Priya: 30
Updated User Profiles: {Ravi=25, Priya=31, Vijay=35}

In this example, LinkedHashMap.replace(K key, V value) is used to update user information conditionally, demonstrating how to handle updates when the key is present.

Conclusion

The LinkedHashMap.replace(K key, V value) method in Java provides a way to replace the value associated with a specific key if it is currently mapped to some value. By understanding how to use this method, you can efficiently update values in your Java applications, ensuring that updates occur only when the key is present in the map. This method makes it easier to manage and manipulate collections of key-value pairs.

Comments