Package com.other.test;
Import Java.util.HashMap; /** * HashMap Simple implementation * @author TXXS * */public class Myownhashmap {/** * Take the size of the array as a private static final int S
ize = 16;
Private Entry table[] = new Entry[size];
/** * We're going to use key and value to define a simple map data structure * It is also used in the list of the bucket conflict processing, if multiple key value pairs have * the same hashcode, but there are different keys, using the pointer "next"/class Entry {
Final String Key;
String value;
Entry Next;
Entry (string k, string v) {key = k;
Value = V;
Public String GetValue () {return value;
public void SetValue (String value) {this.value = value;
Public String Getkey () {return key; }/** returns value based on key in the hashmap of our own implementation, * if there is no value corresponding to key returns null/public Entry get (String k) {int hash = K.hashcode
()% SIZE;
Entry e = Table[hash];
If the corresponding bucket is found, then follow the chain to see if the value corresponding to the key exists while (e!= null) {if (E.key.equals (k)) {return e;
} e = E.next;
return null;
/** * Associates a particular key and value in the map. * If the same key is included, the original old value will be replaced/public VOID put (string k, string v) {int hash=k.hashcode ()%size;
Entry E=table[hash];
if (e!=null) {//check for the same key, if useful new value replaces the old value while (E.next!=null) {if (E.key.equals (k)) {e.value=v;
return;
} E=e.next;
///Add a new element at the end of the chain Entry entryinoldbucket=new Entry (k,v);
E.next=entryinoldbucket;
else{//Create a new barrel Entry entryinnewbucket=new Entry (k,v);
Table[hash]=entryinnewbucket;
}//test public static void main (string[] args) {Myownhashmap tmhm = new Myownhashmap ();
HashMap map = new HashMap ();
Map.put ("Niranjan", "Smts");
Map.put ("Ananth", "SSE");
Map.put ("Niranjan", "SMTS1");
Map.put ("Chandu", "SSE");
Tmhm.put ("Niranjan", "Smts");
Tmhm.put ("Ananth", "SSE");
Tmhm.put ("Niranjan", "SMTS1");
Tmhm.put ("Chandu", "SSE");
Set start time Long starttime = System.nanotime ();
Entry e = Tmhm.get ("Niranjan");
Set end time Long Endtime = System.nanotime ();
Long Duration = Endtime-starttime; System.out.println ("HashMap of your own implementation" +Duration);
Set start time StartTime = System.nanotime ();
String mapresult = (string) map.get ("Niranjan");
Set end Time Endtime = System.nanotime ();
Duration = Endtime-starttime;
System.out.println ("original Java.util.HashMap" + duration);
SYSTEM.OUT.PRINTLN ("Map of your own implementation value:" +e.getvalue ());
System.out.println ("Original Java.util.HashMap value:" +mapresult); }
}