Introduction of HashMap Java in 2022

Introduction of HashMap Java in 2022

HashMap is one of the popular data structure in Java

➡️ HashMap stores data in key, value pair structure.

➡️ HashMap stores unique keys 

Example

class Factance {
  public static void main(String[] args) {

    // create a hashmap
    HashMap factNumbers = new HashMap<>();

    System.out.println("Yello HashMap: " + factNumbers);
    // put() method to add elements
    factNumbers.put("One", 1);
    factNumbers.put("Two", 2);
    factNumbers.put("Three", 3);
    System.out.println("HashMap after put(): " + factNumbers);
  }
}
Yello HashMap: {}
HashMap after put(): {One=1, Two=2, Three=3}

Common HashMap Functions

✅ put(key,value)

Method to add key value into HashMap

class Factance {
  public static void main(String[] args) {

    // create a hashmap
    HashMap factNumbers = new HashMap<>();

    System.out.println("Yello HashMap: " + factNumbers);
    // put() method to add elements
    factNumbers.put("One", 1);
    factNumbers.put("Two", 2);
    factNumbers.put("Three", 3);
    System.out.println("HashMap after put(): " + factNumbers);
  }
}
Yello HashMap: {}
HashMap after put(): {One=1, Two=2, Three=3}

✅ remove(key)

Method to remove entry(key,value) by key from HashMap

import java.util.HashMap;

class Factance {
  public static void main(String[] args) {

    // create a hashmap
    HashMap factNumbers = new HashMap<>();

    System.out.println("Yello HashMap: " + factNumbers);
    // put() method to add elements
    factNumbers.put("One", 1);
    factNumbers.put("Two", 2);
    factNumbers.put("Three", 3);
    System.out.println("HashMap after put(): " + factNumbers);
    
    factNumbers.remove("One");
    System.out.println("\nHashMap after remove(): " + factNumbers);
  }
}
Yello HashMap: {}
HashMap after put(): {One=1, Two=2, Three=3}
HashMap after remove(): {Two=2, Three=3}

✅ get(key)

Method to get value by key

import java.util.HashMap;

class Factance {
  public static void main(String[] args) {

    // create a hashmap
    HashMap factNumbers = new HashMap<>();

    System.out.println("Yello HashMap: " + factNumbers);
    // put() method to add elements
    factNumbers.put("One", 1);
    factNumbers.put("Two", 2);
    factNumbers.put("Three", 3);
    
    Integer twoValue = factNumbers.get("Two");
    System.out.println("\ntwoValue get(): " + twoValue);
    
    
  }
}
Yello HashMap: {}
twoValue get(): 2

Internal working of HashMap

Whenever you call put method

- It calculates hashcode of key by hashCode method and add hashcode into hash table(array)

- Add the value into Linked List after comparing value with equals method

- In case value is already there, it simply updates the value

Whenever you call get method

- It calculates hashcode of key by hashCode meothod 

- Look up hashtable by hashcode

- Iteracte and checks the value in Linked List  using equals method

Collision

- Two keys have same hashcode, it means collision

- HashMap handles collision by storing LinkedIn on top of hash table

Load Factor & Capacity

➡️ Capacity means number of entries it can store

➡️ Load Factor means after filling 60 percent (o.6 load factor) of entries, hash table double size of the original hash table

HashMap numbers = new HashMap<>(8, 0.6f);

capacity 8 means it can store 8 entries

load factor 0.6 means hash table after filling 60 percent, size of original hash table gets double

Important concepts of HashMap

➡️ HashMap put, remove and get happens at speed of O(1)

➡️ HashMap uses hashCode and equals method internally

➡️ In case of collision, if Linked List is filled with a default capacity, Java 8 converts Linked List to self balancing tree to have optimization.

Keywords

java collision hashing hashcode equals linked-list self-balancing-tree load-factor capacity

 

Ejaz Shaikh

I'm passionate about freelancing, technology and healthy lifestyle. I enjoy writing code and writing about code.

Something awesome will come here..