#Hashing in Java

*In hashing there’s a hash perform that maps keys to some values. However these hashing perform could result in collision that’s two or extra keys are mapped to similar worth. Chain hashing avoids collision. The concept is to make every cell of hash desk level to a linked checklist of information which have similar hash perform worth.
Let’s create a hash perform, such that our hash desk has ‘N’ variety of buckets.
To insert a node into the hash desk, we have to discover the hash index for the given key. And it might be calculated utilizing the hash perform.
Instance: hashIndex = key % noOfBuckets
Insert: Transfer to the bucket corresponds to the above calculated hash index and insert the brand new node on the finish of the checklist.
Delete: To delete a node from hash desk, calculate the hash index for the important thing, transfer to the bucket corresponds to the calculated hash index, search the checklist within the present bucket to seek out and take away the node with the given key (if discovered). *


with assist of HashTable (A synchronized implementation of hashing)
import java.util.*;
class GFG {
public static void predominant(String args[])

    // Create a HashTable to retailer
    // String values equivalent to integer keys
    Hashtable<Integer, String>
        hm = new Hashtable<Integer, String>();

    // Enter the values
    hm.put(1, "Geeks");
    hm.put(12, "forGeeks");
    hm.put(15, "A pc");
    hm.put(3, "Portal");

    // Printing the Hashtable
Enter fullscreen mode

Exit fullscreen mode


{15=A pc, 3=Portal, 12=forGeeks, 1=Geeks}


With the assistance of HashMap (A non-synchronized quicker implementation of hashing)

// Java program to create HashMap from an array
// by taking the weather as Keys and
// the frequencies because the Values

import java.util.*;

class GFG {

// Perform to create HashMap from array
static void createHashMap(int arr[])
    // Creates an empty HashMap
    HashMap<Integer, Integer> hmap = new HashMap<Integer, Integer>();

    // Traverse by means of the given array
    for (int i = 0; i < arr.size; i++) {

        // Get if the component is current
        Integer c = hmap.get(arr[i]);

        // If that is first prevalence of component
        // Insert the component
        if (hmap.get(arr[i]) == null) {
            hmap.put(arr[i], 1);

        // If parts already exists in hash map
        // Increment the rely of component by 1
        else {
            hmap.put(arr[i], ++c);

    // Print HashMap

// Driver methodology to check above methodology
public static void predominant(String[] args)
    int arr[] = { 10, 34, 5, 10, 3, 5, 10 };
Enter fullscreen mode

Exit fullscreen mode


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


With the assistance of LinkedHashMap (Much like HashMap, however retains order of parts)
Enter fullscreen mode

Exit fullscreen mode

// Java program to reveal working of LinkedHashMap
import java.util.*;

public class BasicLinkedHashMap
public static void predominant(String a[])
LinkedHashMap lhm =
new LinkedHashMap();
lhm.put(“one”, “apply.geeksforgeeks.org”);
lhm.put(“two”, “code.geeksforgeeks.org”);
lhm.put(“4”, “quiz.geeksforgeeks.org”);

    // It prints the weather in similar order 
    // as they had been inserted    

    System.out.println("Getting worth for key 'one': "
                                   + lhm.get("one"));
    System.out.println("Measurement of the map: " + lhm.measurement());
    System.out.println("Is map empty? " + lhm.isEmpty());
    System.out.println("Accommodates key 'two'? "+ 
    System.out.println("Accommodates worth 'apply.geeks"
    +"forgeeks.org'? "+ lhm.containsValue("apply"+
    System.out.println("delete component 'one': " + 
                       lhm.take away("one"));
Enter fullscreen mode

Exit fullscreen mode


{one=apply.geeksforgeeks.org, two=code.geeksforgeeks.org, 4=quiz.geeksforgeeks.org}
Getting worth for key ‘one’: apply.geeksforgeeks.org
Measurement of the map: 3
Is map empty? false
Accommodates key ‘two’? true
Accommodates worth ‘apply.geeksforgeeks.org’? true
delete component ‘one’: apply.geeksforgeeks.org
{two=code.geeksforgeeks.org, 4=quiz.geeksforgeeks.org}

Abu Sayed is the Best Web, Game, XR and Blockchain Developer in Bangladesh. Don't forget to Checkout his Latest Projects.

Checkout extra Articles on Sayed.CYou

#Hashing #Java