Logo

dev-resources.site

for different kinds of informations.

#Hashing in Java

Published at
11/25/2021
Categories
java
programming
hashing
Author
anuroop123
Categories
3 categories in total
java
open
programming
open
hashing
open
Author
10 person written this
anuroop123
open
#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}

hashing Article's
30 articles in total
Favicon
What is Hashing? A Complete Guide for Developers and Security Professionals
Favicon
The Evolution of Hashing Algorithms: From MD5 to Modern Day
Favicon
Pattern 9: Hashing
Favicon
Lithe Hash: Um MΓ³dulo Robusto para Hashing Seguro de Senhas
Favicon
The Bcrypt Algorithm for Secure Password Hashing
Favicon
Sorting Algorithms That Use Hash Tables
Favicon
π–€π—‡π–Όπ—‹π—’π—‰π—π—‚π—ˆπ—‡ 𝖺𝗇𝖽 π–§π–Ίπ—Œπ—π—‚π—‡π—€: π–§π—ˆπ— 𝖳𝗁𝖾𝗒 π–―π—‹π—ˆπ—π–Ύπ–Όπ— π–Έπ—ˆπ—Žπ—‹ 𝖣𝖺𝗍𝖺 𝖣𝗂𝖿𝖿𝖾𝗋𝖾𝗇𝗍𝗅𝗒
Favicon
Two Unconventional Ways to store Passwords: Honeywords & Rock Salt
Favicon
Difference Between Encryption and Hashing πŸ”πŸ”‘
Favicon
Basic File Integrity Monitoring System
Favicon
Lithe Hash: A Robust Module for Secure Password Hashing
Favicon
Comparative Analysis of Password Hashing Algorithms: Argon2, bcrypt, scrypt, and PBKDF2
Favicon
Cryptojs vs. Bcryptjs: Which password hashing method should you trust?
Favicon
Distributed Hash Generation Algorithm in Python β€” Twitter Snowflake Approach
Favicon
Understanding Bcrypt Rounds: Balancing Security and Performance
Favicon
Wednesday Links - Edition 2023-05-03 πŸ‡΅πŸ‡±πŸ“œ
Favicon
Understanding Hashing Algorithms: A Beginner's Guide
Favicon
Hashing in Blockchain Technology: How it Ensures Data Integrity
Favicon
Using Hash Codes as Unique Identifiers
Favicon
What is Password Hashing Algorithm?
Favicon
Hashing
Favicon
πŸ”How to encrypt variables in NodeJS
Favicon
Lava lamps securing the Web?πŸ€·β€β™‚οΈ
Favicon
What is the difference between encryption, hashing and salting?
Favicon
Security in The Blockchain
Favicon
Hashing Algorithms and creating a simple file integrity monitor (FIM)
Favicon
Hashing, Explain it to me like I'm 5.
Favicon
#Hashing in Java
Favicon
How to hash a password in Go
Favicon
Sony FAIL : why it is absolutely necessary to encrypt passwords

Featured ones: