Saturday, March 13, 2010

Not like HashMap ..yes..but basic utility of Hashing

package com.newproject.model;

class Hash
{
T key;
V value;
Hash hashLink;

Hash(T argOne, V argTow)
{
key = argOne;
value = argTow;
}

public String toString()
{
return " - key : " + key + " and value : " + value;
}
}

interface IHashStore
{
public void put(T argStrKey, V argStrValue);
public Hash get(T argStrKey);
public void remove(T argStrKey);
}

public class HashStorage
implements IHashStore

{
Hash[] hashArry = null;
Hash[] head = null;
static int[] iCnt = new int[10];

HashStorage()
{
hashArry = new Hash[10];
head = new Hash[10];
}

public String toString()
{
StringBuffer sb = new StringBuffer();
for (int iCnt = 0; iCnt < 10; iCnt++)
{
if (head[iCnt] == null)
{
continue;
}
if (head[iCnt] != null)
{
sb.append("\n");
sb.append("head[iCnt].key is " + head[iCnt].key);
sb.append("\n");
}
while (head[iCnt].hashLink != null)
{
head[iCnt] = head[iCnt].hashLink;
sb.append("Travelling through links inside a array .... key is - " + head[iCnt].key);
sb.append("\n");

}
}
return sb.toString();
}

public void elementInsert(T argKey, V argValue)
{
int lenth = String.valueOf(argKey.hashCode()).length();
if (argKey != null)
{
if (hashArry[lenth] == null)
{
hashArry[lenth] = new Hash(argKey, argValue);
head[lenth] = hashArry[lenth];
System.out.println("Inserted Data inside Array at position [" + lenth + "] and It's value is " + head[lenth]);
}
else
{
System.out.println("Already Element is available at position [" + lenth + "]");
while (hashArry[lenth].hashLink != null)
{
iCnt[lenth]++;
System.out.print("After Node data key :" + hashArry[lenth].hashLink.key + " ");
hashArry[lenth] = hashArry[lenth].hashLink;
}
hashArry[lenth].hashLink = new Hash(argKey, argValue);
System.out.println(", next data inserted is - " + hashArry[lenth].hashLink.key);
}
}
}

public Hash elementFetch(T argKey)
{
int lenth = String.valueOf(argKey.hashCode()).length();
if (argKey != null)
{
if (head[lenth].hashLink == null)
{
return hashArry[lenth];
}
else
{
System.out.println("Already Element is available at position [" + lenth + "]");
while (head[lenth].hashLink != null)
{

if (argKey instanceof Integer || argKey instanceof Long || argKey instanceof Double || argKey instanceof Float )
{
if (head[lenth].key == argKey)
{
System.out.println("**** return value is - " + head[lenth].value);
return head[lenth];
}
}
else if (argKey instanceof String)
{
if (((String) head[lenth].key).equalsIgnoreCase((String) argKey))
{
System.out.println("**** return value is - " + head[lenth].value);
return head[lenth];
}
}
head[lenth] = head[lenth].hashLink;
}
}
}
return null;
}

public void put(T argKey, V argValue)
{
elementInsert(argKey, argValue);
}

public Hash get(T argKey)
{
return elementFetch(argKey);
}

public static void main(String[] args)
{
IHashStore hs = new HashStorage();
hs.put(112l, 11);
hs.put(221l, 11);
hs.put(12l, 14);
hs.put(44l, 15);
hs.put(24l, 12);
hs.put(34l, 22);
hs.put(45l, 44);
hs.put(234l, 66);
hs.put(345l, 12);
Object obj = hs.get(112l);
System.out.println("Obj " + obj);
System.out.println(hs);
}
@Override
public void remove(T argStrKey)
{
// To Do...
}

}

No comments:

Post a Comment