How to Update value of HashMap using key In Java

How to Update value of HashMap using key In Java

Java HashMap

  • The HashMap class in Java is a part of the java.util package and provides a hash table implementation of the Map interface.
  • It maps keys to values, and allows for efficient lookups and retrievals of values based on their keys.
    • You must be familiar with following topics to understand the correspond example Such as: Java HashMap and Java  Lamda Expressions.
  • To understand the how to to Update value of HashMap using key, Read the Complete Article.

Steps to Update value of HashMap using key:

  • Here are the steps to Update value of HashMap using key in Java:
    • Get the HashMap: Declare a HashMap and initialize it with values, or get a reference to an existing HashMap.

    • Get the key: Get the key for which you want to update the value.

    • Update the value: Use the put method of the HashMap to update the value of the key. The put method takes two arguments, the first is the key and the second is the value.

The HashMap class provides several methods for accessing and modifying the elements stored in the map, including:

  • get(Object key): returns the value associated with the specified key.
  • put(K key, V value): associates the specified value with the specified key.
  • remove(Object key): removes the key and its associated value from the map.
  • clear(): removes all of the mappings from the map.
  • containsKey(Object key): returns true if the map contains a mapping for the specified key.

Let’s look at the Java Program to Update value of HashMap using key to perform certain operations.

Example 1: Java Program to Check weather an Alphabet is Vowel or Consonant

Run

import java.util.HashMap;

public class Main
{
  public static void main (String[]args)
  {
    HashMap < String, Integer > map = new HashMap <> ();
    map.put ("Ashish", 28);
    map.put ("Ayush", 22);

    // updating the value of the key "John"
    map.put ("Ayush", 26);

    System.out.println (map);
  }
}

Output

 {Ashish=28, Ayush=26}

Example 2 : Java Program to Check weather an Alphabet is Vowel or Consonant

Run

import java.util.HashMap;

public class Main {
    public static void main(String[] args) {
        HashMap<String, String> map = new HashMap<>();
        map.put("Name", "Ayush");
        map.put("City", "Pune");

        // updating the value of the key "Name"
        map.put("Name", "Ashish");

        System.out.println(map);
    }
}

Output

{City=Pune, Name=Ashish}

Example 3: Java Program to Update value of HashMap using computeIfPresent() 

Run
import java.util.HashMap;

public class Main
{
  public static void main (String[]args)
  {
    HashMap < String, Integer > map = new HashMap <> ();
    map.put ("Ashish", 25);
    map.put ("Harsh", 18);

    // updating the value of the key "John"
    map.computeIfPresent ("Harsh", (key, value)->value * 3);

    System.out.println (map);
  }
}

Output

{Ashish=25, Harsh=54}

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription