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

Generate Parenthesis | Intuition + Code | Recursion Tree | Backtracking | Java

Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…

2 months ago

Square Root of Integer

Given an integer A. Compute and return the square root of A. If A is…

1 year ago

Build Array From Permutation

Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…

1 year ago

DSA: Heap

A heap is a specialized tree-based data structure that satisfies the heap property. It is…

1 year ago

DSA: Trie

What is a Trie in DSA? A trie, often known as a prefix tree, is…

1 year ago

Trees: Lowest Common Ancestor

What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…

1 year ago