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.
Java HashMap:
A HashMap is a dynamic data structure, which means that it can grow and shrink as the number of elements stored in it changes.
The size of a HashMap is not fixed, and it can expand as necessary to accommodate new elements.
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.
Java Lambda Expressions:
Java lambda expressions are a way to create anonymous functions (i.e., functions without a name) that can be passed as a value to other functions or used as a short block of code to perform a specific task.
They were introduced in Java 8 and provide a concise way to write functional-style code.
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
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}
Explanation:
The program starts by declaring a HashMap map and initializing it with two key-value pairs: "Ashish" and 28, and "Ayush" and 22.
The value of the key "Ayush" is then updated using the put method of the HashMap.
The put method takes two arguments: the key and the value.
In this case, the key is "Ayush" and the value is 28.
This updates the value of the key "Ayush" in the HashMap map from 22 to 28.
Example 2 : Java Program to Check weather an Alphabet is Vowel or Consonant
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}
Explanation:
This program starts by declaring a HashMap map and initializing it with two key-value pairs: "Name" and "Ayush", and "City" and "Pune".
The value of the key "Name" is then updated using the put method of the HashMap.
The put method takes two arguments: the key and the value.
In this case, the key is "Name" and the value is "Ashish".
This updates the value of the key "Name" in the HashMap map from "Ayush" to "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}
Explanation:
This program starts by declaring a HashMap map and initializing it with two key-value pairs: "Ashish" and 25, and "Harsh" and 18.
The value of the key "Harsh" is then updated using the computeIfPresent method of the HashMap.
The computeIfPresent method takes two arguments: the key, and a BiFunction that defines the operation to be performed when the key is present.
In this case, the key is "Harsh" and the operation is (key, value) -> value * 3, which multiplies the existing value by 3.
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
Login/Signup to comment