HashMap Class In Java

What is a Hashmap?

HashMap class is one of the popular classes among all collection classes, and it stores elements in the form of key and value pairs and is declared as HashMap <Key, Value>. If the key is provided later, its corresponding value can be easily retrieved from the Hashmap, but it should be unique. This means we can not use duplicate data for keys in the hashmap. However, the Hashmap is not synchronized; hence, we get unreliable results using multiple threads on the HashMap object.

Key Points about HashMap

  • It is a part of java.util package.
  • It Stores elements in the form of key-value pairs, and the keys should be unique.
  • It does not allow duplicate keys.
  • A class can have one null key and multiple null values.
  • HashMap class extends the abstract class AbstractMap class, which does not complete the implementation of the Map interface.
  • It’s not an ordered collection, which means it does not return the key and values in the same order in which they were inserted.
  • If you want to retrieve the value, then you need to use the associated key.
  • It implements Cloneable and Serializable interfaces.
  • It is not threaded-safe, but we can make it thread-safe by using
    Map m = Collections.synchronizedMap(new HashMap(“XXXX”));

Constructors in HashMap Class

HashMap provides four constructors, and the access modifier of each is public:

  • HashMap(): The default constructor creates an instance of HashMap with an initial capacity of 16 and a load factor of 0.75.
  • HashMap(int initial capacity): It creates a HashMap instance with a specified initial size and load factor of 0.75.
  • HashMap(int initial capacity, float loadFactor): It creates a HashMap instance with a specified initial size and specified load factor.
  • HashMap(Map map): It creates an instance of HashMap with the same mappings as a specified map.

HashMap Methods

How do you verify hashmap elements by value In Java?

package com.java.Softwaretestingblog;
import java.util.HashMap;
public class VerifyHashmapElements {
   public static void main(String[] args) {
      // TODO Auto-generated method stub
      HashMap<String, String> hm = new HashMap<String, String>();  
      //add key-value pair to hashmap 
      hm.put("first", "FIRST INSERTED"); 
      hm.put("second", "SECOND INSERTED"); 
      hm.put("third","THIRD INSERTED"); 
      System.out.println(hm); 
      if(hm.containsValue("SECOND INSERTED"))
      {
         System.out.println("The hashmap contains value SECOND INSERTED"); 
      }
      else
      {
         System.out.println("The hashmap does not contains value SECOND INSERTED"); 
      }
      if(hm.containsValue("first"))
      {
         System.out.println("The hashmap contains value first"); 
      }
      else
      {
         System.out.println("The hashmap does not contains value first");
      }
   }
}

Output:

{third=THIRD INSERTED, first=FIRST INSERTED, second=SECOND INSERTED}
The hashmap contains value SECOND INSERTED
The hashmap does not contains value first

Write a Program to Add and retrieve Elements In Hashmap Java.

package com.java.collection.hashmap;
import java.util.HashMap;
import java.util.Set;

public class MyHashMapKeys {

   public static void main(String[] args) 
   {
      HashMap<String, String> hm = new HashMap<String, String>(); 
      
      //add key-value pair to hashmap 
      hm.put("first", "FIRST INSERTED"); 
      hm.put("second", "SECOND INSERTED"); 
      hm.put("third","THIRD INSERTED"); 
      
      System.out.println(hm); 
      Set<String> keys = hm.keySet(); 
      for(String key: keys)
      {
         System.out.println(key);
      }

   }

}

Java Hashmap In Java: Write a Program to Add New Values In Java Hashmap With Example?

package com.java.collection.hashmap; 

import java.util.HashMap;
import java.util.Map.Entry;
import java.util.Set;

public class MyHashMapEntrySet {

   public static void main(String[] args) 
   {
      HashMap<String, String> hm = new HashMap<String, String>(); 
      //add key-value pair to hashmap 
      
      hm.put("first", "FIRST INSERTED"); 
      hm.put("second", "SECOND INSERTED"); 
      hm.put("third","THIRD INSERTED"); 
      System.out.println(hm); 
      
      //getting value for the given key from hashmap 
      Set<Entry<String, String>> entires = hm.entrySet(); 
      for(Entry<String,String> ent:entires)
      {
         System.out.println(ent.getKey()+" ==> "+ent.getValue()); 
      }

   }

}

Write a Program to Find Basic Hashmap operations in Java.

package com.java.Softwaretestingblog;
import java.util.HashMap;
public class BasicHashmapOperation {
   public static void main(String[] args) {
      // TODO Auto-generated method stub
      HashMap<String, String> hm = new HashMap<String, String>(); 
      //add key-value pair to hashmap 
      hm.put("first", "FIRST INSERTED"); 
      hm.put("second", "SECOND INSERTED"); 
      hm.put("third","THIRD INSERTED"); 
      System.out.println(hm); 
      //getting value for the given key from hashmap 
      System.out.println("Value of second: "+hm.get("second")); 
      System.out.println("Is HashMap empty? "+hm.isEmpty()); 
      hm.remove("third"); 
      System.out.println(hm); System.out.println("Size of the HashMap: "+hm.size());
   }
}

Output:

{third=THIRD INSERTED, first=FIRST INSERTED, second=SECOND INSERTED}
Value of second: SECOND INSERTED
Is HashMap empty? false
{first=FIRST INSERTED, second=SECOND INSERTED}
Size of the HashMap: 2

I love open-source technologies and am very passionate about software development. I like to share my knowledge with others, especially on technology that's why I have given all the examples as simple as possible to understand for beginners. All the code posted on my blog is developed, compiled, and tested in my development environment. If you find any mistakes or bugs, Please drop an email to softwaretestingo.com@gmail.com, or You can join me on Linkedin.

Leave a Comment