Friday, August 19, 2011

Hashtable implementation in JAVA

I came across many forums and blogs where I saw questions to implement Hashtable but there is no satisfactory answer for that question. I tried here to give the answer, how to implement hashtable in java using Array.

First lets go in hashtable fundametal. The Hashtable stores key value pair and the retrieval and insert of element time complexity is O(1).
To store the elements in hashtable there is need to calculate hashkey which is use to store value at specific index on array. The function "hash" in program calculates the hash code once the hash code is calculated modulo is taken by the size of Array to map the key at particular index of the array. remember to minimize collision take size at prime number.

There are possibility of collision on elements at particular index if the hash value calculated same for the two different key and hence the old value will be over written in by new value. To avoid this situation there are multiple way to implement the logic but here we are avoiding this logic. We will focus how hashtable interal structure and how does it works. I have implemented two main important method "get" and "put"

package com.interview;

public class MyHashTable<K, V> {
/* take the capacity as prime number to reduce the collision */
private static int SIZE = 31;
/* initialize array to store value */
private V[] tableValues = (V[]) new Object[SIZE];

public synchronized V put(K key, V value) {
if (value == null) {
throw new NullPointerException();
}
int index = hash(key.hashCode()) % SIZE;
tableValues[index] = value;
return value;
}

public synchronized V get(K key) {
int index = hash(key.hashCode()) % SIZE;
return tableValues[index];
}

/**
* method calculates the hash code
*/
private synchronized int hash(int h) {
h ^= (h >>> 20) ^ (h >>> 12);
return h ^ (h >>> 7) ^ (h >>> 4);
}

public static void main(String arg[]) {
MyHashTable<String, String> table = new MyHashTable<String, String>();
/* store 10 values in Hashtable */
for (int i = 0; i < 10; i++) {
table.put("key" + i, "value" + i);
}
/* retrive values */
for (int i = 0; i < 10; i++) {
System.out.println(table.get("key" + i));
}
}
}

12 comments:

  1. Good article I have also shared my experience as How HashMap works in Java , let me know how do you find it.

    Thanks
    Javin

    ReplyDelete
  2. Could you explain ::

    private synchronized int hash(int h) {

    h ^= (h >>> 20) ^ (h >>> 12);

    return h ^ (h >>> 7) ^ (h >>> 4);

    }

    ReplyDelete
    Replies
    1. That's Java's implementation of hash function. The writer has tried to not reinvent the wheel with his own function.

      Delete
  3. Your implementation doesn't handle the key collision. No matter how good is your hash function, a collision will occur sooner or later.

    ReplyDelete
  4. what if you have too many entries in your hashtable, say 100 key,value pairs

    ReplyDelete
  5. This is not a correct implementation of a hash table. If you don't handle collisions you'll lose your entries which is not acceptable.

    ReplyDelete
  6. Kindly read the explanation of the logic, above the code. The author has mentioned, the logic of collision is not handled in this code. If you'll know the correct implementation of the hashtable, kindly provide it with proper handling to collision factor.

    ReplyDelete
  7. how do you implement method keySet() to get all the keys atonce

    ReplyDelete
  8. if can't handle collisions,then your implementation is never complete :|

    ReplyDelete
  9. if can't handle collisions,then your implementation is never complete :|

    ReplyDelete
  10. Love it - great stuff Dharmesh! There's a good interview question here involving hash tables:

    hash table interview question

    ReplyDelete