WikiGalaxy

Personalize

Introduction to Hashing

Understanding Hash Functions

Hash functions are algorithms that convert input data of any size into a fixed-size string of text, typically a sequence of numbers and letters. This output is known as the hash value or hash code.

Properties of Good Hash Functions

A good hash function should have properties like determinism, uniformity, defined range, and speed. It should minimize collisions (two different inputs producing the same output).

Applications of Hashing

Hashing is widely used in various applications such as data retrieval, cryptography, and data integrity verification. It is essential for efficient data management and security.

Collision Resolution Techniques

Common techniques to handle collisions include chaining, open addressing, and double hashing. These methods ensure that the hash table remains efficient even when collisions occur.


import java.util.HashMap;
class HashFunctionExample {
  public static void main(String[] args) {
    HashMap map = new HashMap<>();
    map.put(1, "Apple");
    map.put(2, "Banana");
    System.out.println(map.get(1));
  }
}
    

Example: Simple HashMap Usage

This example demonstrates the basic use of a HashMap in Java, which uses hashing to store key-value pairs efficiently.

Console Output:

Apple

Chaining in Hash Tables

Concept of Chaining

Chaining is a technique used to resolve collisions in a hash table by maintaining a list of all elements that hash to the same location.

Advantages of Chaining

Chaining allows multiple elements to exist at the same index, effectively handling collisions without requiring additional space for probing.


import java.util.LinkedList;
class ChainingExample {
  LinkedList[] hashTable;
  
  ChainingExample(int size) {
    hashTable = new LinkedList[size];
    for (int i = 0; i < size; i++) {
      hashTable[i] = new LinkedList<>();
    }
  }
  
  void insert(String key) {
    int index = key.hashCode() % hashTable.length;
    hashTable[index].add(key);
  }
}
    

Implementing Chaining

This code snippet illustrates how chaining can be implemented using linked lists in a custom hash table.

Open Addressing

What is Open Addressing?

Open addressing is a collision resolution method where, upon encountering a collision, alternative cells are checked until an empty one is found.

Types of Open Addressing

Common strategies include linear probing, quadratic probing, and double hashing, each with its own way of finding the next available slot.


class OpenAddressingExample {
  String[] hashTable;
  
  OpenAddressingExample(int size) {
    hashTable = new String[size];
  }
  
  void insert(String key) {
    int index = key.hashCode() % hashTable.length;
    while (hashTable[index] != null) {
      index = (index + 1) % hashTable.length;
    }
    hashTable[index] = key;
  }
}
    

Linear Probing Example

This example shows how linear probing works, where the next slot is checked sequentially until an empty slot is found.

Double Hashing

Double Hashing Explained

Double hashing uses two hash functions to calculate the index for a key, reducing clustering and improving distribution.


class DoubleHashingExample {
  String[] hashTable;
  
  DoubleHashingExample(int size) {
    hashTable = new String[size];
  }
  
  void insert(String key) {
    int index = key.hashCode() % hashTable.length;
    int stepSize = 5 - (key.hashCode() % 5);
    while (hashTable[index] != null) {
      index = (index + stepSize) % hashTable.length;
    }
    hashTable[index] = key;
  }
}
    

Reducing Clustering

Double hashing minimizes clustering by using a second hash function to determine the step size, leading to better performance.

Cryptographic Hash Functions

Purpose of Cryptographic Hashes

Cryptographic hash functions are designed to ensure data integrity and security. They produce unique outputs for unique inputs, making them ideal for encryption and digital signatures.


import java.security.MessageDigest;
class CryptographicHashExample {
  public static String hash(String input) throws Exception {
    MessageDigest md = MessageDigest.getInstance("SHA-256");
    byte[] hash = md.digest(input.getBytes("UTF-8"));
    StringBuilder hexString = new StringBuilder();
    for (byte b : hash) {
      hexString.append(String.format("%02x", b));
    }
    return hexString.toString();
  }
}
    

Example: SHA-256 Hashing

This example demonstrates how to use the SHA-256 algorithm to create a cryptographic hash of a string in Java.

logo of wikigalaxy

Newsletter

Subscribe to our newsletter for weekly updates and promotions.

Privacy Policy

 • 

Terms of Service

Copyright © WikiGalaxy 2025