Data Structure & Algorithm in Java

  1. Home
  2. Docs
  3. Data Structure & Alg...
  4. Searching and Hashing
  5. Hashing in java.util.

Hashing in java.util.

In Java’s java.util package, hashing is extensively used in various data structures and utility classes. Here are some common uses of hashing in java.util related to Data Structures and Algorithms (DSA):

Hash Map<K, V> and Hash Table <K, V>: These classes implement hash tables to store key-value pairs. They use hashing to efficiently store and retrieve elements based on the keys.

  • Hash Set: This class implements a set based on a hash table. It uses hashing to store unique elements efficiently and support operations like adding, removing, and checking for containment in constant time on average.
  • Identity Hash Map<K, V>: This class implements a hash table that uses reference equality in place of object equality when comparing keys. It uses hashing to store and retrieve key-value pairs based on the identity hash code of the keys.
  • Linked Hash Map<K, V>: This class extends hash map to maintain the order of elements in which they were inserted. It uses hashing for quick access to elements and a linked list to maintain their order.

These classes rely on hashing to provide efficient data storage and retrieval, making them essential tools for implementing various algorithms and data structures in Java programs.

»With help of Hash Table (A synchronized implementation of hashing)

// Java program to demonstrate working of HashTable
import java.util.*;
class GFG {
    public static void main(String args[])
        // Create a HashTable to store 
        // String values corresponding to integer keys
        Hashtable<Integer, String>
            hm = new Hashtable<Integer, String>();
        // Input the values
        hm.put(1, "Bim");
        hm.put(12, "forBimstudies");
        hm.put(15, "A computer");
        hm.put(3, "Portal");
        // Printing the Hashtable


{15=A computer, 3=Portal, 12=forBimstudies, 1=Bim}

»With the help of HashMap (A non-synchronized faster implementation of hashing)

// Java program to create HashMap from an array
// by taking the elements as Keys and
// the frequencies as the Values
import java.util.*;
class GFG {
    // Function to create HashMap from array
    static void createHashMap(int arr[])
        // Creates an empty HashMap
        HashMap<Integer, Integer> hmap = new HashMap<Integer, Integer>();
        // Traverse through the given array
        for (int i = 0; i < arr.length; i++) {
            // Get if the element is present
            Integer c = hmap.get(arr[i]);
            // If this is first occurrence of element
            // Insert the element
            if (hmap.get(arr[i]) == null) {
                hmap.put(arr[i], 1);
            // If elements already exists in hash map
            // Increment the count of element by 1
            else {
                hmap.put(arr[i], ++c);
        // Print HashMap
    // Driver method to test above method
    public static void main(String[] args)
        int arr[] = { 10, 34, 5, 10, 3, 5, 10 };


{34=1, 3=1, 5=2, 10=3}

»With the help of Linked Hash Map(Similar to HashMap, but keeps order of elements)

// Java program to demonstrate working of LinkedHashMap 
import java.util.*; 
public class BasicLinkedHashMap 
    public static void main(String a[]) 
        LinkedHashMap<String, String> lhm = 
                       new LinkedHashMap<String, String>(); 
        lhm.put("one", ""); 
        lhm.put("two", ""); 
        lhm.put("four", ""); 
        // It prints the elements in same order  
        // as they were inserted     
        System.out.println("Getting value for key 'one': "
                                       + lhm.get("one")); 
        System.out.println("Size of the map: " + lhm.size()); 
        System.out.println("Is map empty? " + lhm.isEmpty()); 
        System.out.println("Contains key 'two'? "+  
        System.out.println("Contains value 'practice.geeks"
        +""? "+ lhm.containsValue("practice"+ 
        System.out.println("delete element 'one': " +  


Getting value for key 'one':
Size of the map: 3
Is map empty? false
Contains key 'two'? true
Contains value ''? true
delete element 'one':

How can we help?

Leave a Reply

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