The realization principle of HashMap

Source: Internet
Author: User

To understand the HashMap implementation principle, you need to know the hash table (hash table) first.

A, hash list

A hash table is a data structure that can quickly find the desired object, and the hash table calculates an integer for each object, called the hash code, which is an integer generated by the instance field of the object. Having different data domain objects will result in different hash codes, and the hash codes in Java are implemented by the Hashcode method.

In Java, the hash list is implemented with linked lists and arrays, each of which is called a bucket, to find the position of the object in the table, it is necessary to calculate its hash code, and then to the total number of buckets, the result is the index of the bucket that holds the element. For example, a bucket of size 16, the hash code is 12 of the object, should be stored in his 12%16=12 bucket, if the hash code is 28 of the object, 28%16=12, should also be stored in the bucket 12th, just add elements on the original basis of the line.

Second, Analog HashMap

HashMap implementation principle is a hash table, but hashmap need to judge the key can not be repeated, if repeated, with the new value overwrite the old value. I use the LinkedList array simulation to implement the put and get methods in HashMap. LinkedList is loaded with the entry inner class, which has the key and the Value property

 Packagetest;Importjava.util.LinkedList; Public classMymap<k,v> {    Privatelinkedlist[] table; Private intsize;  PublicMyMap () { This(16); }     PublicMyMap (intcapacity) {Table=NewLinkedlist[capacity]; } @SuppressWarnings ("Unchecked")     Public voidput (K key,v value) {Entry e=NewEntry (Key,value); //get the hash value so that the hash value is positive       int hash = Key.hashcode ()%table.length; hash = hash>0?hash:- Hash; LinkedList Link=Table[hash]; if(link = =NULL) {Table[hash]=NewLinkedList ();        Table[hash].add (e); }Else{            //judge whether the key is duplicated, there is the overlay, no add             for(intI=0;i<link.size (); i++) {Entry Entry=(Entry) link.get (i); if(Entry.key.equals (key)) {Entry.value=value; return;        }} table[hash].add (e); } size++; }         PublicV get (K key) {inthash = Key.hashcode ()%table.length; Hash= hash>0?hash:-Hash; LinkedList Link=Table[hash]; if(link!=NULL){             for(intI=0;i<link.size (); i++) {Entry Entry=(Entry) link.get (i); if(Entry.key.equals (key)) {return(V) Entry.value; }            }        }        return NULL; }         Public intsize () {returnsize; }        classEntry<k,v>{K key;                V value;  PublicEntry () {} PublicEntry (K key,v value) { This. Key =key;  This. Value =value; }} @SuppressWarnings ("Unchecked")     Public Static voidMain (string[] args) {MyMap MyMap=NewMyMap (); Mymap.put (1, "AAAA"); Mymap.put (1, "BBBB"); Mymap.put (2, "CCCC"); System.out.println (Mymap.get (2));    System.out.println (Mymap.size ()); }}

Reference: 107 Shang School _ Gao _java300 Set the most complete course _ container _ Implement Hashmap_map implementation _ hash algorithm implementation _ using arrays and linked lists

The realization principle of HashMap

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.