Simple implementation of Java HashMap

Source: Internet
Author: User
Tags map data structure
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); }

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.