Hashed data structures

Introduction

Common way of indexing data is by using hash maps which we use to map keys to values.

The indexing of keys to values is built by using a hash function to compute which bucket or location our data will be stored in.

Image result for hashed data structure

If two pieces of data are hashed to the same location, it is called "collision".

The Hash Function
A hash function is basically a function that takes arbitrary data and returns a fixed length output.
We usually call the output of a hash function as hash code. It is one of the most widely used data structures in modern programming.

Properties of a hash function :
1. Deterministic :- A given input always generates the same output
2. Uniform :- The output should be probable in a given range

 public long DJBHash(String str)
   {
      long hash = 5381;

      for(int i = 0; i < str.length(); i++)
      {
         hash = ((hash << 5) + hash) + str.charAt(i);
      }

      return hash;
   }

Perfect Hashing
If all the keys are known ahead of time, a perfect hash function can be used to create a perfect hash table that has no collisions. 

Since we do not know all the keys in advance there are always chances of collisions. In the real world this can be compared to two people sharing the same birthday.

Handling Collisions
Separate chaining is a method of handling collisions in a hashed data structure.

Image result for separate chaining hash table


Comments

Popular posts from this blog

Collection Framework - HashSet And LinkedHashSet class

Collection Framework - Cursors in Java