Hashmap and hashtable Learning

Source: Internet
Author: User

1. hashmap

1) Data Structure of hashmap

Hashmap is a combination of arrays and linked lists (called "linked list hash" in the data structure), as shown in:

When we put an element in a hashmap, we first calculate the hash value of the Key to the position (subscript) of the element in the array ), then you can put this element in the corresponding position. If the seat where this element is located already contains other elements, the elements in the same position will be stored in the form of a linked list, and the newly added elements will be placed in the chain head, the first join is placed at the end of the chain.

2) Use

 1   Import  Java. util. hashmap;  2   Import  Java. util. iterator; 3   Import  Java. util. Map;  4   5   6   Public   Class  Hash {  7   8       Void  Test (){  9 Map <string, string> map = New Hashmap <string, string> (); 10 Map. Put ("Rajib Sarma", "100" );  11 Map. Put ("Fig", "200 "); //  The value "100" is replaced by "200 ".  12 Map. Put ("sazid Ahmed", "200" );  13 Map. Put ("Sam ", Null ); //  In hashmap, null can be used as a key. Such a key has only one; one or more keys can correspond to null values.  14 Map. Put ("Rose ",Null  );  15   16 Iterator iter = map. entryset (). iterator (); //  Returns the set view of the ing relationships contained in the ing.  17       While  (ITER. hasnext ()){  18 Map. Entry entry = (Map. Entry) ITER. Next ();  19 Object key = Entry. getkey ();  20  System. Out. println (key. tostring ());  21 Object val = Entry. getvalue ();  22           //  System. Out. println (Val. tostring ());  23   }  24   }  25       26       Public   Static   Void Main (string [] ARGs)  27   {  28 Hash hash = New  Hash ();  29   Hash. Test ();  30   }  31 }

2. Differences between hashtable and hashmap

First, inheritance is different.

 
Public class hashtable extends dictionary implements Map
Public class hashmap extends actmap implements Map

Second

Methods In hashtable are synchronized, while those in hashmap are not synchronized by default.Hashtable can be used directly in a multi-thread concurrent environment, but you need to add synchronization to use hashmap.

Third

In hashtable, keys and values cannot contain null values.

In hashmap, null can be used as a key,Such a key has only one; one or more keys can correspond to a value of null. When the get () method returns a null value, it can indicate that the key does not exist in hashmap or that the corresponding value of the key is null. Therefore, the get () method cannot be used to determine whether a key exists in the hashmap. Instead, the containskey () method should be used to determine whether a key exists in the hashmap.

Fourth, the internal implementation of the two traversal methods is different.

Hashtable and hashmap both use iterator. For historical reasons, hashtable also uses the enumeration method.

Fifth

The hashcode of an object is directly used by hashtable. Hashmap recalculates the hash value.

Sixth

Hashtable and hashmap are two internal implementation methods: the initial size of the array and the method of resizing. In hashtable, the default size of the hash array is 11, and the increment is old * 2 + 1. The default size of the hash array in hashmap is 16, and it must be an index of 2.

Hashmap and hashtable Learning

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.