Performing Various Operations on HashMap
1. Adding Elements in HashMap in Java
In order to add an element to the map, we can use the put() method. However, the insertion order is not retained in the Hashmap. Internally, for every element, a separate hash is generated and the elements are indexed based on this hash to make it more efficient.
Java
// Java program to add elements // to the HashMap import java.io.*; import java.util.*; class AddElementsToHashMap { public static void main(String args[]) { // No need to mention the // Generic type twice HashMap<Integer, String> hm1 = new HashMap<>(); // Initialization of a HashMap // using Generics HashMap<Integer, String> hm2 = new HashMap<Integer, String>(); // Add Elements using put method hm1.put( 1 , "Geeks" ); hm1.put( 2 , "For" ); hm1.put( 3 , "Geeks" ); hm2.put( 1 , "Geeks" ); hm2.put( 2 , "For" ); hm2.put( 3 , "Geeks" ); System.out.println( "Mappings of HashMap hm1 are : " + hm1); System.out.println( "Mapping of HashMap hm2 are : " + hm2); } } |
Mappings of HashMap hm1 are : {1=Geeks, 2=For, 3=Geeks} Mapping of HashMap hm2 are : {1=Geeks, 2=For, 3=Geeks}
2. Changing Elements in HashMap in Java
After adding the elements if we wish to change the element, it can be done by again adding the element with the put() method. Since the elements in the map are indexed using the keys, the value of the key can be changed by simply inserting the updated value for the key for which we wish to change.
Java
// Java program to change // elements of HashMap import java.io.*; import java.util.*; class ChangeElementsOfHashMap { public static void main(String args[]) { // Initialization of a HashMap HashMap<Integer, String> hm = new HashMap<Integer, 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); } } |
Initial Map {1=Geeks, 2=Geeks, 3=Geeks} Updated Map {1=Geeks, 2=For, 3=Geeks}
3. Removing Element from Java HashMap
In 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 HashMap import java.io.*; import java.util.*; class RemoveElementsOfHashMap{ public static void main(String args[]) { // Initialization of a HashMap Map<Integer, String> hm = new HashMap<Integer, 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); } } |
Mappings 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 HashMap
We 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.HashMap import java.util.HashMap; import java.util.Map; public class TraversalTheHashMap { public static void main(String[] args) { // initialize a HashMap HashMap<String, 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.Entry<String, Integer> e : map.entrySet()) System.out.println( "Key: " + e.getKey() + " Value: " + e.getValue()); } } |
Key: vaibhav Value: 20 Key: vishal Value: 10 Key: sachin Value: 30
HashMap in Java
In Java, HashMap is a part of Java’s collection since Java 1.2. This class is found in java.util package. It provides the basic implementation of the Map interface of Java. HashMap in Java stores the data in (Key, Value) pairs, and you can access them by an index of another type (e.g. an Integer). One object is used as a key (index) to another object (value). If you try to insert the duplicate key in HashMap, it will replace the element of the corresponding key.