Performing Operations using Map Interface and HashMap Class
Since Map is an interface, it can be used only with a class that implements this interface. Now, let’s see how to perform a few frequently used operations on a Map using the widely used HashMap class. And also, after the introduction of Generics in Java 1.5, it is possible to restrict the type of object that can be stored in the map.
1. Adding Elements
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.
Example
Java
// Java program to demonstrate // the working of Map interface import java.util.*; class GFG { public static void main(String args[]) { // Default Initialization of a // Map Map<Integer, String> hm1 = new HashMap<>(); // Initialization of a Map // using Generics Map<Integer, String> hm2 = new HashMap<Integer, String>(); // Inserting the Elements hm1.put( 1 , "Geeks" ); hm1.put( 2 , "For" ); hm1.put( 3 , "Geeks" ); hm2.put( new Integer( 1 ), "Geeks" ); hm2.put( new Integer( 2 ), "For" ); hm2.put( new Integer( 3 ), "Geeks" ); System.out.println(hm1); System.out.println(hm2); } } |
{1=Geeks, 2=For, 3=Geeks} {1=Geeks, 2=For, 3=Geeks}
2. Changing Element
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.
Example
Java
// Java program to demonstrate // the working of Map interface import java.util.*; class GFG { public static void main(String args[]) { // Initialization of a Map // using Generics Map<Integer, String> hm1 = new HashMap<Integer, String>(); // Inserting the Elements hm1.put( new Integer( 1 ), "Geeks" ); hm1.put( new Integer( 2 ), "Geeks" ); hm1.put( new Integer( 3 ), "Geeks" ); System.out.println( "Initial Map " + hm1); hm1.put( new Integer( 2 ), "For" ); System.out.println( "Updated Map " + hm1); } } |
Initial Map {1=Geeks, 2=Geeks, 3=Geeks} Updated Map {1=Geeks, 2=For, 3=Geeks}
3. Removing Elements
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.
Example
Java
// Java program to demonstrate // the working of Map interface import java.util.*; class GFG { public static void main(String args[]) { // Initialization of a Map // using Generics Map<Integer, String> hm1 = new HashMap<Integer, String>(); // Inserting the Elements hm1.put( new Integer( 1 ), "Geeks" ); hm1.put( new Integer( 2 ), "For" ); hm1.put( new Integer( 3 ), "Geeks" ); hm1.put( new Integer( 4 ), "For" ); // Initial Map System.out.println(hm1); hm1.remove( new Integer( 4 )); // Final Map System.out.println(hm1); } } |
{1=Geeks, 2=For, 3=Geeks, 4=For} {1=Geeks, 2=For, 3=Geeks}
4. Iterating through the Map
There are multiple ways to iterate through the Map. The most famous way is to use a for-each loop and get the keys. The value of the key is found by using the getValue() method.
Example
Java
// Java program to demonstrate // the working of Map interface import java.util.*; class GFG { public static void main(String args[]) { // Initialization of a Map // using Generics Map<Integer, String> hm1 = new HashMap<Integer, String>(); // Inserting the Elements hm1.put( new Integer( 1 ), "Geeks" ); hm1.put( new Integer( 2 ), "For" ); hm1.put( new Integer( 3 ), "Geeks" ); for (Map.Entry mapElement : hm1.entrySet()) { int key = ( int )mapElement.getKey(); // Finding the value String value = (String)mapElement.getValue(); System.out.println(key + " : " + value); } } } |
1 : Geeks 2 : For 3 : Geeks
5. Count the Occurrence of numbers using Hashmap
In this code, we are using putIfAbsent() along with Collections.frequency() to count the exact occurrence of numbers. In many programs, you need to count the occurrence of a particular number or letter. You use the following approach to solve those types of problems
Java
// Java program to Count the Occurrence // of numbers using Hashmap import java.util.*; class HelloWorld { public static void main(String[] args) { int a[] = { 1 , 13 , 4 , 1 , 41 , 31 , 31 , 4 , 13 , 2 }; // put all elements in arraylist ArrayList<Integer> aa = new ArrayList(); for ( int i = 0 ; i < a.length; i++) { aa.add(a[i]); } HashMap<Integer, Integer> h = new HashMap(); // counting occurrence of numbers for ( int i = 0 ; i < aa.size(); i++) { h.putIfAbsent(aa.get(i), Collections.frequency( aa, aa.get(i))); } System.out.println(h); } } |
{1=2, 2=1, 4=2, 41=1, 13=2, 31=2}
Map Interface in Java
In Java, Map Interface is present in java.util package represents a mapping between a key and a value. Java Map interface is not a subtype of the Collection interface. Therefore it behaves a bit differently from the rest of the collection types. A map contains unique keys.
Geeks, the brainstormer should have been why and when to use Maps.
Maps are perfect to use for key-value association mapping such as dictionaries. The maps are used to perform lookups by keys or when someone wants to retrieve and update elements by keys. Some common scenarios are as follows:
- A map of error codes and their descriptions.
- A map of zip codes and cities.
- A map of managers and employees. Each manager (key) is associated with a list of employees (value) he manages.
- A map of classes and students. Each class (key) is associated with a list of students (value).