Working with HashMap (Insertion, Deletion, Iteration) in Java

In this Java tutorial, we will learn working with HashMap in Java.

A map is an object that stores information as a key- value pair. Given a key, we can find its value. Key and value both are objects. The key must be unique but the value can have duplicate values. The Map interface maps unique keys to values. Map interface is generic and is defined as:

interface Map<K, V>

Here, K is the data type of key whereas V specifies the data type of value.
We cannot loop through a map using a for loop or iterator because it doesn’t implement Iterable interface. But we can find collection view of a map. The entrySet() method can be used. It returns a set that contains elements stored in map.

Common methods defined by Map:

  • boolean containsKey(K key): It returns True if a specified key is present in the map.
  • V get(K key): It returns the value associated with the key.
  • V put(K key, V value): It puts an entry in the map, overwriting any previous value associated with the key.
  • Set<Map.Entry<K, V>> entrySet( ): It returns a Set that contains the entries in the map.
  • V remove(K key): It removes the entry whose key equals k.
  • int size( ): It returns the number of key/value pairs in the map.
  • Collection<V> values( ): It returns a collection containing the values in the map.

The HashMap class

The HashMap class extends AbstractMap class and implements the Map interface. It uses a technique called hashing. Hashing is a technique of converting a large string to a small String that represents same String. Due to this technique, the time complexity of HashMap improves. It makes the operations of get() and put() of constant time.
So, HashMap is very time efficient.

The HashMap does not guarantee the order of elements. Therefore, the order of elements in which they are added and in which they are read is not the same.

Example illustrating working with HashMap in Java

import java.util.*;
class HashMapDemo {
public static void main(String args[]) {
// Create a hash map.
HashMap<String, Double> hm = new HashMap<String, Double>();
// Put elements to the map
hm.put("John Doe", new Double(3434.34));
hm.put("Tom Smith", new Double(123.22));
hm.put("Jane Baker", new Double(1378.00));
hm.put("Tod Hall", new Double(99.22));
hm.put("Ralph Smith", new Double(-19.08));
// Get a set of the entries.
Set<Map.Entry<String, Double>> set = hm.entrySet();
// Display the set.
for(Map.Entry<String, Double> me : set) {
System.out.print(me.getKey() + ": ");
System.out.println(me.getValue());
}
System.out.println();

//Removing an element from HashMap
hm.remove("Tod Hall");

// Deposit 1000 into John Doe's account.
double balance = hm.get("John Doe");
hm.put("John Doe", balance + 1000);
System.out.println("John Doe's new balance: " +
hm.get("John Doe"));
}
}

Output:

Ralph Smith: -19.08
Tom Smith: 123.22
John Doe: 3434.34
Tod Hall: 99.22
Jane Baker: 1378.0
John Doe’s new balance: 4434.34

Also read:

 

Leave a Reply

Your email address will not be published. Required fields are marked *