Java

Java – HashMap With Examples

HashMap, also known as HashMapKey, Value> or HashMapK, V>, is a Map-based collection class that is used to store Key & value pairs. Similar to Java’s Hashtable class, HashMap allows null values and is unsynchronized (null values and null key).

It does not return the keys and values in the same order in which they were placed into the HashMap since it is not an ordered collection. The saved keys and values are not sorted. To utilize the HashMap class and methods, you must import java.util.HashMap or its super class.

HashMap

Let’s go in depth about HashMap.

  • The key in key-value pairs has to be distinct.
  • It lacks synchronization. You may synchronize it in any case.
  • does not preserve the sequence of insertion.
  • not sort the components

Null keys and values are supported. However, there can be only one null key. The number of null values is unlimited.

HashMap class hierarchy

How to declare HashMap in Java?

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

K: It stands for the key’s type in a key-value pair.
V: It stands for the value’s type in a key-value pair.

For instance: It is possible to define a hash map with integer keys and string values like follows:

HashMap<Integer, String> hmap = new HashMap<Integer, String>();

HashMap in Java Examples

1. Adding elements to HashMap

To add new key-value pairs to the HashMap, utilize the put() function of the HashMap class. The entrySet() function is being used to iterate the hash map. An analogous Set is returned by this function. This is done in order to send the key-value pairs to the Map.Entry, which has the getKey() and getvalue() methods that may be used to print HashMap key-value pairs. Key-value pairs are written as the elements of the hash map are explored using a for loop.

import java.util.*;
public class JavaExample{
  public static void main(String args[]){
    HashMap<Integer,String> hMap=new HashMap<>();
    hMap.put(101,"Cricket");
    hMap.put(105,"Hockey");
    hMap.put(111,"Basketball");

    System.out.println("HashMap elements: ");
    for(Map.Entry mEntry : hMap.entrySet()){
      System.out.print("key: "+ mEntry.getKey() + " & Value: ");
      System.out.println(mEntry.getValue());
    }
  }
}

Output:
HashMap in Java Example output

2. Checking duplicate key insertion in HashMap

We’re attempting to add an element with a duplicate key in this case. The hash map already has the key value 111, which is used in the new element (111,”Karate”). HashMap modified the value of the previously existing element with key “111” rather than creating this new element.

import java.util.*;
public class JavaExample{
  public static void main(String args[]){
    HashMap<Integer,String> hMap=new HashMap<>();
    hMap.put(101,"Cricket");
    hMap.put(105,"Hockey");
    hMap.put(111,"Basketball");
    hMap.put(111,"Karate"); //adding element with duplicate key

    System.out.println("HashMap elements: ");
    for(Map.Entry mEntry : hMap.entrySet()){
      System.out.print("key: "+ mEntry.getKey() + " & Value: ");
      System.out.println(mEntry.getValue());
    }
  }
}

Output:
HashMap duplicate key example

3. HashMap remove() method Example

import java.util.*;
public class JavaExample{
  public static void main(String args[]){
    HashMap<Integer,String> hMap=new HashMap<>();
    hMap.put(101,"Cricket");
    hMap.put(105,"Hockey");
    hMap.put(111,"Basketball");

    //this will remove the key-value pair where
    //the value of the key is 101
    hMap.remove(101);

    System.out.println("HashMap elements: ");
    for(Map.Entry mEntry : hMap.entrySet()){
      System.out.print("key: "+ mEntry.getKey() + " & Value: ");
      System.out.println(mEntry.getValue());
    }
  }
}

Output:

HashMap elements: 
key: 105 & Value: Hockey
key: 111 & Value: Basketball

4. HashMap replace() method Example

import java.util.*;
public class JavaExample{
  public static void main(String args[]){
    HashMap<Integer,String> hMap=new HashMap<>();
    hMap.put(101,"Cricket");
    hMap.put(105,"Hockey");
    hMap.put(111,"Basketball");

    //this will update the value of key-value pair
    //where the key is 105
    hMap.replace(105, "Kickboxing");

    System.out.println("HashMap elements: ");
    for(Map.Entry mEntry : hMap.entrySet()){
      System.out.print("key: "+ mEntry.getKey() + " & Value: ");
      System.out.println(mEntry.getValue());
    }
  }
}

Output:

HashMap elements: 
key: 101 & Value: Cricket
key: 105 & Value: Kickboxing
key: 111 & Value: Basketball

Internal working of HashMap in Java

Hashing is a method that is used by HashMap internally to create indexes for keys. This indexing facilitates quicker record searches. As a result, activities like search, update, and deletion may respond more quickly.

When we execute the following line, we call the HashMap put() function. It uses the hash function to determine the key’s hash code, which in this instance is 101. The hashing method is used to determine this hash code. This hash code is given an index. This key-value combination is identified only by this index.

HashMap<Integer,String> hMap=new HashMap<>();
hMap.put(101,"Cricket");

Hash Collision occurs when a duplicate key is added to the hash map. HashMap responds to this by replacing the previous value with the present one.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button