Comment
Author: Admin | 2025-04-27
Updated value for the key for which we wish to change. Java // Java program to change// elements of HashMapimport java.io.*;import java.util.*;class ChangeElementsOfHashMap { public static void main(String args[]) { // Initialization of a HashMap HashMapInteger, String> hm = new HashMapInteger, String>(); // Change Value using put method hm.put(1, "Geeks"); hm.put(2, "Geeks"); hm.put(3, "Geeks"); System.out.println("Initial Map " + hm); hm.put(2, "For"); System.out.println("Updated Map " + hm); }}OutputInitial Map {1=Geeks, 2=Geeks, 3=Geeks}Updated Map {1=Geeks, 2=For, 3=Geeks}3. Removing Element from Java HashMapIn order to remove an element from the Map, we can use the remove() method. This method takes the key value and removes the mapping for a key from this map if it is present in the map. Java // Java program to remove// elements from HashMapimport java.io.*;import java.util.*;class RemoveElementsOfHashMap{ public static void main(String args[]) { // Initialization of a HashMap MapInteger, String> hm = new HashMapInteger, String>(); // Add elements using put method hm.put(1, "Geeks"); hm.put(2, "For"); hm.put(3, "Geeks"); hm.put(4, "For"); // Initial HashMap System.out.println("Mappings of HashMap are : " + hm); // remove element with a key // using remove method hm.remove(4); // Final HashMap System.out.println("Mappings after removal are : " + hm); }}OutputMappings of HashMap are : {1=Geeks, 2=For, 3=Geeks, 4=For}Mappings after removal are : {1=Geeks, 2=For, 3=Geeks}4. Traversal of Java HashMapWe can use the Iterator interface to traverse over any structure of the Collection Framework. Since Iterators work with one type of data we use Entry to resolve the two separate types into a compatible format. Then using the next() method we print the entries of HashMap. Java // Java program to traversal a// Java.util.HashMapimport java.util.HashMap;import java.util.Map;public class TraversalTheHashMap { public static void main(String[] args) { // initialize a HashMap HashMapString, Integer> map = new HashMap(); // Add elements using put method map.put("vishal", 10); map.put("sachin", 30); map.put("vaibhav", 20); // Iterate the map using // for-each loop for (Map.EntryString, Integer> e : map.entrySet()) System.out.println("Key: " + e.getKey() + " Value: " + e.getValue()); }}OutputKey: vaibhav Value: 20Key: vishal Value: 10Key: sachin Value: 30Time and Space ComplexityHashMap provides constant time complexity for basic operations, get and put if the hash function is properly written and it disperses the elements properly among the buckets. Iteration over HashMap depends on the capacity of HashMap and the number of key-value pairs. Basically, it is directly proportional to the capacity + size. Capacity is the number of buckets in HashMap. So it is not a good idea to keep a high number of buckets in HashMap initially.MethodsTime ComplexitySpace ComplexityAdding Elements in HashMapO(1)O(N)Removing Element from HashMapO(1)O(N)Extracting Element from JavaO(1)O(N)DSA Problems On HashMapCount Frequencies in an ArrayMost Frequent ElementCount distinct elements in every window of size KCheck if two arrays are equal or not2 Sum
Add Comment