Categories: Java

HashMap class in Java Collection Framework

Since Java 1.2, HashMap<K, V> class has been part of the Java collection. This class can be found in java.util package. It implements the Java Map interface in its most basic form. If you try to insert the duplicate key, it will replace the associated key’s element. The key index makes it simple to conduct actions such as updating, deleting, etc.

HashMap is similar to HashTable except that it is not synchronized. It also allows for the storage of null keys, however, there should only be one null key object and an unlimited number of null values.

HashMap class Declaration:
public class HashMap<K,V>extends AbstractMap<K,V>implements Map<K,V>,Cloneable, Serializable
Important points to remember:
  • The Java HashMap stores values based on the key.
  • Java HashMap only has unique keys.
  • Java HashMap can have one or more null keys and values.
  • Furthermore, Java HashMap does not support synchronization.
  • Java HashMap does not keep track of the order.
  • Its initial default capacity is 16.
HashMap constructors are as follows:

HashMap has four constructors, each with a public access modifier, which are as follows:

1)HashMap() :

HashMap<K, V> hm = new HashMap<K, V>();

2)HashMap (int initialCapacity):

HashMap<K, V> hm = new HashMap<K, V>(int initialCapacity);

3)HashMap (int initialCapacity, float loadFactor):

HashMap<K, V> hm = new HashMap<K, V>(int initialCapacity, int  loadFactor);

4)HashMap (Map map):

HashMap<K, V> hm = new HashMap<K, V>(Map map);
Example: Creating and Adding Elements To HashMap

import java.io.*;
import java.util.*;

class Coderz {
 public static void main(String args[])
 {
  Map<Integer, String> hmobj = new HashMap<>();

  // Add Elements using put method
  hmobj.put(1, "A");
  hmobj.put(2, "B");
  hmobj.put(3, "C");

  // Initialization of a HashMap
  // using Generics
  HashMap<Integer, String> hmobj2
   = new HashMap<Integer, String>(hmobj);

  System.out.println("Mappings of HashMap hmobj are : "
      + hmobj);
  
  System.out.println("Mapping of HashMap hmobj2 are : "
      + hmobj);
 }
}
Output:
Mappings of HashMap hmobj are : {1=A, 2=B, 3=C}
Mapping of HashMap hmobj2 are : {1=A, 2=B, 3=C}
Example: Changing Elements of HashMap

import java.io.*;
import java.util.*;
class Coderz {
 public static void main(String args[])
 {

  // Initialization of a HashMap
  HashMap<Integer, String> hobj
   = new HashMap<Integer, String>();

  // Change Value using put method
  hobj.put(1, "A");
  hobj.put(2, "B");
  hobj.put(3, "C");

  System.out.println("Initial Map " + hobj);

  hobj.put(2, "E");

  System.out.println("Updated Map " + hobj);
 }
}
Output:
Initial Map {1=A, 2=B, 3=C}
Updated Map {1=A, 2=E, 3=C}
Example: Deleting Elements of HashMap

import java.io.*;
import java.util.*;
class Coderz {
 public static void main(String args[])
 {

  // Initialization of a HashMap
  HashMap<Integer, String> hobj
   = new HashMap<Integer, String>();

  // Change Value using put method
  hobj.put(1, "A");
  hobj.put(2, "B");
  hobj.put(3, "C");

  System.out.println("Initial Map " + hobj);

  hobj.remove(2);

  System.out.println("Updated Map " + hobj);
 }
}
Output:
Initial Map {1=A, 2=B, 3=C}
Updated Map {1=A, 3=C}
Example: Traversing Elements of HashMap

import java.io.*;
import java.util.*;
class Coderz {
 public static void main(String args[])
 {

  // Initialization of a HashMap
  HashMap<Integer, String> hobj
   = new HashMap<Integer, String>();

  // Change Value using put method
  hobj.put(1, "A");
  hobj.put(2, "B");
  hobj.put(3, "C");

  for (Map.Entry<Integer, String> e : hobj.entrySet())
            System.out.println("Key: " + e.getKey()
                               + " Value: " + e.getValue());
 }
}
Output:
Key: 1 Value: A
Key: 2 Value: B
Key: 3 Value: C

Note: also read about the LinkedHashSet class in Java Collection Framework

Follow Me

If you like my post, please follow me to read my latest post on programming and technology.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

Share
Published by
Rabecca Fatima

Recent Posts

Select a Random Element from a Stream

You are given a stream of elements that is too large to fit into memory.…

23 hours ago

Estimate π Using Monte Carlo Method

The formula for the area of a circle is given by πr². Use the Monte…

3 weeks ago

Longest Substring with K Distinct Characters

Given an integer k and a string s, write a function to determine the length…

3 weeks ago

Staircase Climbing Ways

There is a staircase with N steps, and you can ascend either 1 step or…

4 weeks ago

Autocomplete System Implementation

Build an autocomplete system that, given a query string s and a set of possible…

4 weeks ago

Job Scheduler Implementation

Design a job scheduler that accepts a function f and an integer n. The scheduler…

4 weeks ago