Update Value of HashMap Using Key in Java



In this article, we will understand how to update the value of HashMap using the key in Java. Java HashMap is a hash table-based implementation of Java's Map interface. It is a collection of key-value pairs.

Problem Statement

Write a program to update the value of HashMap using a key. Below is a demonstration of the same ?

Input

Input HashMap: {Java=1, Scala=2, Python=3}

Output

The HashMap with the updated value is: {Java=1, Scala=12, Python=3}

Different Approaches

Following are the different approaches to updating the value of HashMap using key ?

Using the main() method

Following are the steps to update the value of HashMap using key ?

  • Import HashMap from the java.util package.
  • In the main method, create a HashMap named input_map and add key-value pairs: "Java"=1, "Scala"=2, and "Python"=3.
  • Print the original HashMap.
  • Retrieve the value associated with the key "Scala", increment it by 10, and update the entry in input_map.
  • Print the HashMap with the updated value.

Example

Here, we bind all the operations together under the main() method ?

import java.util.HashMap;
public class Demo {
   public static void main(String[] args) {
      System.out.println("The required packages have been imported");
      HashMap<String, Integer> input_map = new HashMap<>();
      input_map.put("Java", 1);
      input_map.put("Scala", 2);
      input_map.put("Python", 3);
      System.out.println("The HashMap is defined as: " + input_map);
      int value = input_map.get("Scala");
      value = value + 10;
      input_map.put("Scala", value);
      System.out.println("\nThe HashMap with the updated value is: " + input_map);
   }
}

Output

The required packages have been imported
The HashMap is defined as: {Java=1, Scala=2, Python=3}

The HashMap with the updated value is: {Java=1, Scala=12, Python=3}

Using encapsulation

Here are the steps to update a HashMap value using encapsulation ?

  • Import HashMap from the java.util package.
  • Define a static method update that takes a HashMap and a String as parameters to retrieve the value for the specified key, increment it by 10, and update the entry in the HashMap, printing the updated HashMap.
  • In the main method, create a HashMap named input_map and add key-value pairs: "Java"=1, "Scala"=2, and "Python"=3.
  • Print the original HashMap.
  • Call the update method with input_map and the key "Scala" as arguments.

Example

Here, we encapsulate the operations into functions exhibiting object-oriented programming ?

import java.util.HashMap;
public class Demo {
   static void update(HashMap<String, Integer> input_map, String update_string){
      int value = input_map.get(update_string);
      value = value + 10;
      input_map.put("Scala", value);
      System.out.println("\nThe HashMap with the updated value is: " + input_map);
   }
   public static void main(String[] args) {
      System.out.println("The required packages have been imported");
      HashMap<String, Integer> input_map = new HashMap<>();
      input_map.put("Java", 1);
      input_map.put("Scala", 2);
      input_map.put("Python", 3);
      System.out.println("The HashMap is defined as: " + input_map);
      String update_string = "Scala";
      update(input_map, update_string);
   }
}

Output

The required packages have been imported
The HashMap is defined as: {Java=1, Scala=2, Python=3}

The HashMap with the updated value is: {Java=1, Scala=12, Python=3}
Updated on: 2024-11-04T18:43:50+05:30

826 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements