123 Eng

Engineering the engineers™


Latest Jobs   Forum Map

 


Home
Source Codes
Engineering Colleges

Training  Reports
Seminar Reports
Placement Papers

Forums

   Computer Science / IT
   Electronics
   Electrical
   Mechanical
   Chemical
   Civil

   CAT / MBA

   GMAT / Foreign MBA
Latest Jobs

Engineering Jobs / Technical Jobs
Management Jobs

Sitemap
Terms of use

Displaying  Source Code(s)  
 

 
A simple hashtable in Java

--------------------------------------------------------------------------------

Description : A simple hashtable in Java

import java.util.*;


class Entry{
String name;
String desc;
}

class Hash{
private final int HASHSIZE=101;
private ArrayList<LinkedList<Entry>> hashtab;

public Hash(){
hashtab=new ArrayList<LinkedList<Entry>>(HASHSIZE);
for(int i=0;i<HASHSIZE;i++)
hashtab.add(new LinkedList<Entry>());
}

private int hash(String s){
int h=0;
for(int j=0;j<s.length();j++)
h=(int)s.charAt(j)+h*31;
return h%HASHSIZE;
}

private int lookup(String name){
int i=hash(name);
LinkedList<Entry> l=hashtab.get(i);
for(int j=0;j<l.size();j++)
if((l.get(j)).name.equals(name))
return j;
return -1;
}

public void install(String name,String desc){
int e;
Entry ent=new Entry();
ent.name=name;
ent.desc=desc;
int hi=hash(name);
if((e=lookup(name))!=-1)
(hashtab.get(hi)).set(e,ent);
else
(hashtab.get(hi)).add(ent);
}

public String restore(String name){
int i;
if((i=lookup(name))!=-1){
int hi=hash(name);
return ((hashtab.get(hi)).get(i)).desc;
}
else
return null;
}
}

class hash_test{
public static void main(String args[]){
Hash h=new Hash();
h.install("NAME","Sourav");
h.install("ADD","Sinagor");

h.install("PHONE","26300788");

System.out.println("PHONE : "+h.restore("PHONE"));
h.install("PHONE","9433120451");
System.out.println("PHONE : "+h.restore("PHONE"));
System.out.println("HOBBY : "+h.restore("HOBBY"));
}
}

 

Contribute content or training reports / feedback / Comments
job placement papers
All rights reserved © copyright 123ENG