DEV Community

Cover image for #Hashing in Java
NANDYALA ANUROOP
NANDYALA ANUROOP

Posted on • Edited on

#Hashing in Java

Hashing

*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

Method-1:

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 System.out.println(hm); } 
Enter fullscreen mode Exit fullscreen mode

}
Output:

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

Method-2:

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

}
Output:

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

Method-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", "practice.geeksforgeeks.org");
lhm.put("two", "code.geeksforgeeks.org");
lhm.put("four", "quiz.geeksforgeeks.org");

 // 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" +"forgeeks.org'? "+ lhm.containsValue("practice"+ ".geeksforgeeks.org")); System.out.println("delete element 'one': " + lhm.remove("one")); System.out.println(lhm); } 
Enter fullscreen mode Exit fullscreen mode

}
Output:

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

Top comments (0)