DEV Community

Cover image for #Hashing in Java

Posted on • Updated on

#Hashing in Java


*In hashing there is a hash function that maps keys to some values. But these hashing function may lead to collision that is two or more keys are mapped to same value. Chain hashing avoids collision. The idea is to make each cell of hash table point to a linked list of records that have same hash function value.
Let’s create a hash function, such that our hash table has ‘N’ number of buckets.
To insert a node into the hash table, we need to find the hash index for the given key. And it could be calculated using the hash function.
Example: hashIndex = key % noOfBuckets
Insert: Move to the bucket corresponds to the above calculated hash index and insert the new node at the end of the list.
Delete: To delete a node from hash table, calculate the hash index for the key, move to the bucket corresponds to the calculated hash index, search the list in the current bucket to find and remove the node with the given key (if found). *

Methods to implement Hashing in Java


with help of HashTable (A synchronized implementation of hashing)
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, "Geeks");
    hm.put(12, "forGeeks");
    hm.put(15, "A computer");
    hm.put(3, "Portal");

    // Printing the Hashtable
Enter fullscreen mode Exit fullscreen mode


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


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 };
Enter fullscreen mode Exit fullscreen mode


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


With the help of LinkedHashMap (Similar to HashMap, but keeps order of elements)
Enter fullscreen mode Exit fullscreen mode

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

public class BasicLinkedHashMap
public static void main(String a[])
LinkedHashMap lhm =
new LinkedHashMap();
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': " + 
Enter fullscreen mode Exit fullscreen mode


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

Discussion (0)