Data Structure & Algorithm in Java

⌘K
  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
        System.out.println(hm);
    }
}

Output:

{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
        System.out.println(hmap);
    }
 
    // Driver method to test above method
    public static void main(String[] args)
    {
        int arr[] = { 10, 34, 5, 10, 3, 5, 10 };
        createHashMap(arr);
    }
}

Output:

{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", "practice.bimstudies.com"); 
        lhm.put("two", "code.bimstudies.com"); 
        lhm.put("four", "www.bimstudies.com"); 
   
        // It prints the elements in same order  
        // as they were inserted     
        System.out.println(lhm); 
   
        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'? "+  
                                  lhm.containsKey("two")); 
        System.out.println("Contains value 'practice.geeks"
        +"bimstudies.com"? "+ lhm.containsValue("practice"+ 
        ".bimstudies.com")); 
        System.out.println("delete element 'one': " +  
                           lhm.remove("one")); 
        System.out.println(lhm); 
    } 
} 

Output:

{one=practice.bimstudies.com, two=code.bimstudies.com, four=www.bimstudies.com}
Getting value for key 'one': practice.bimstudies.com
Size of the map: 3
Is map empty? false
Contains key 'two'? true
Contains value 'practice.bimstudies.com'? true
delete element 'one': practice.bimstudies.com
{two=code.bimstudies.com, four=www.bimstudies.com}

How can we help?

Leave a Reply

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