Replace Key and Value in HashMap with Identical Key and Different Values in Java



In Java, the HashMap class is a part of the Java.util package and provides a convenient way to store key-value pairs. Sometimes, We need to update the values of existing keys while keeping the keys identical.

This can be easily achieved using the put() method, replace() method, and computeIfPresent() method which allows to replace the value with a specific key.

Creating a HashMap and setting key-value pair ?

Map<Integer, String>map = new HashMap<>();
map.put(10, "A");
map.put(20, "B");
map.put(30, "C");

Now, let's set a different value for an identical key using the put(), replace(), and computeIfPresent() methods ?

map.put(10, "T");
map.replace(10, "T");
map.computeIfPresent(10, (key, value) -> "T");

Replacing keys and values in HashMap with identical keys and different values is quite easy. Let's learn the following methods:

Replacing key and value Using Put() Method

The put() method in Java is used with the HashMap class to insert a key-value pair into the map. If the specified key is already present, this method updates the value associated with the key.

Example

In the following program, we demonstrate how to create and update a HashMap in Java. Initially, it adds key-value pairs {10="A", 20="B", 30="H"} to the map. Then, it updates the value for the key 10 from "A" to "T" using the put() method.

import java.util.HashMap;
import java.util.Map;
public class Main {
   public static void main(String args[]) {
      Map<Integer, String>map = new HashMap<>();
      map.put(10, "A");
      map.put(20, "B");
      map.put(30, "H");
      System.out.println("Map = " + map);
      map.put(10, "T");
      System.out.println("Updated Map = " + map);
   }
}

Output

The above program displayed the following output ?

Map ={20=B, 10=A, 30=H}
Updated Map = {20=B, 10=T, 30=H}

Replacing key and value Using Replace() Method

The replace() method in Java is used to replace the value with a specific key in a HashMap. If the key exists, the method replaces the current value with the new one or else null if the key is not present.

Example

Following is another example that creates a HashMap and adds the key-value pairs, then updates the value for key 10 using the replace() method.

import java.util.HashMap;
import java.util.Map;

public class Main {
   public static void main(String args[]) {
      Map<Integer, String> map = new HashMap<>();
      map.put(10, "A");
      map.put(20, "B");
      map.put(30, "H");
      System.out.println("Map = " + map);

      // Replace the value with key 10
      map.replace(10, "T");
      System.out.println("Updated Map = " + map);
   }
}

Output

The above program produce the following result ?

Map = {20=B, 10=A, 30=H}
Updated Map = {20=B, 10=T, 30=H}

Replacing key and value Using ComputeIfPresent() Method

The computeIfPresent() method in Java is used to update the value with a specific key and allows you to recompute the value for a key using the given remapping function.

Example

The program creates a HashMap, adds key-value pairs, and updates the value for key 10 to "T" using the computeIfPresent() method.

import java.util.HashMap;
import java.util.Map;

public class Main {
   public static void main(String args[]) {
      Map<Integer, String> map = new HashMap<>();
      map.put(10, "A");
      map.put(20, "B");
      map.put(30, "H");
      System.out.println("Map = " + map);

      // Replace the value with key 10 using computeIfPresent
      map.computeIfPresent(10, (key, value) -> "T");
      System.out.println("Updated Map = " + map);
   }
}

Output

The above program produce the following result ?

Map ={20=B, 10=A, 30=H}
Updated Map = {20=B, 10=T, 30=H}
Revathi Satya Kondra
Revathi Satya Kondra

Technical Content Writer, Tutorialspoint

Updated on: 2024-12-30T19:17:32+05:30

366 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements