HashMap

HashMap is used to store data in a key value manner. It provides mapping between key and respected values.

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

Where param:
K (Key) : Keys maintained by Map of Type K

V(Value) : Mapped values of Type V


Key points of HashMap

  • HashMap is part of the Collections Framework
  • Found in java.util package Therefore required to import java.util.HashMap; before using HashMap.
  • HashMap inherits the AbstractMap class
  • HashMap implements the Map interface.
HashMap<K,V> extends AbstractMap<K,V> implements Map<K,V>, Cloneable, Serializable

1. What is the difference between hashtable and hashmap?

ID Topic Hashtable HashMap
1 Synchronization Hashtable is synchronized, Meaning that in a multithreaded environment , it does not allow more than one thread to access and process the Hashtable in any particular time. HashMap is not synchronized. In a multithreaded environment, there can be more than one thread can access and process the same HashMap.
2 Null Key Hashtable does not allow you to store any null key. HashMap allows you to store one Null key.
3 Null Value Hashtable does not allow you to store any null value. HashMap allows you to store multiple null values.
4 Iteration Traversing stored value can be achieved by using Enumerator and Iterator. In Hashtable is traversed by Enumerator and Iterator. Traversing stored value can be achieved by using Iterator.
5 Inherits It Inherits Dictionary class. It inherits AbstractMap class.
6 Thread-Safe It is thread-safe. Also two or more threads can share Hashtable. It is not-thread safe. It can nott be shared between many threads in general. It requires synchronization code to be shared. Example synchronizedMap: a method of Collections used to convert hashmap into synchronized map.
7 Execution Time Slow than HashMap HashMap is faster than Hashtable

2. How to iterate hashmap in java? Learn number of ways to iterate hashmap.

Hashmap can be loop through by using
  • For Loop Iteration using Iterator
  • While Loop iteration with Iterator
  • forEach Loop
  • Stream

Go through How to Iterate (Loop) Hashmap in Java With Example for more details.

3. How to fetch value from hashmap by given key?

java.util.HashMap.get method use to fetch value from hashmap.

Here is the example code
package javaapplication;

import java.util.Map;

import java.util.HashMap;


public class JavaApplication {


    public static void main(String[] args) {

        // HashMap object with list of employees with their name and department
        Map<String, String> employees = new HashMap<>();

        // keys and values (Name, Department)
        employees.put("John", "HR");
        employees.put("Eric", "IT");
        employees.put("Joe", "LEGAL");
        
         System.out.format("key (Name): %s, value (Department): %s %n", "Eric", employees.get("Eric"));
         // Output
          key (Name): Eric, value (Department): IT 
        
    } 

}

4. what will hashmap return if mapping key does not exist?

null

It will return null by using java.util.Map.get() . Here is the example code.

 // keys and values (Name, Department)
        employees.put("John", "HR");
        employees.put("Eric", "IT");
        employees.put("Joe", "LEGAL");
        
        
         System.out.format("key (Name): %s, value (Department): %s %n", "Eric", employees.get("noKey"));
        Output: key (Name): Eric, value (Department): null