Java Reading notes---HashMap and hashtable

Source: Internet
Author: User

First of all, Hashmap,hashmap is a class, and all classes in Java inherit from an object class. The Hashcode () method is defined in the object class, in other words, any class will have this hashcode () method.

So the Key.hashcode () function calls the hash function with the key key value type and returns the int hash value.

First look at the two methods defined in HashMap

method One:Static Final intHash (Object key) {//jdk1.8 & jdk1.7     inth; //h = Key.hashcode () is the first step to take the Hashcode value//h ^ (H >>> 16) for the second step high-level participation operation     return(Key = =NULL) ? 0: (H = key.hashcode ()) ^ (H >>> 16);} Method Two:Static intIndexfor (intHintLength) {//jdk1.7 Source Code, jdk1.8 No this method, but the implementation of the same principle     returnH & (LENGTH-1);//The third step takes the modulo operation}

For example, we define the map Map=new HashMap (), and when we use Map.put ("name", "Xiaoming") to add a key-value pair, we call method one's hash (Object key) method, which returns "name" The value of the Hashcode value with "name". Hashcode () shifts the values after the bitwise XOR operation to the right of 16 bits, and this value is also quite a layer of encryption on the basis of the common Hashcode () method. (In fact, the string-type Hashcode () method has been reproduced in the string class, but is not implemented in the object class and is implemented in the underlying C language)

The Indexfor () method specifies the position of the HashMap object in the array. In addition, when we put elements in the HashMap, it is worthwhile to place the element in the array (i.e. subscript) according to the hash of the key, and then we can put the element in the corresponding position. If there are other elements in the seat of this element, then the elements in the same seat will be stored in the form of a list, and the newly added ones are placed in the chain head, and the first ones are placed at the end of the chain. As shown

The roles of HashMap and Hashtable are roughly the same, with different points as shown below

The methods in 1.Hashtable are synchronous, and the methods in HashMap are not synchronized by default. In the context of multi-threaded concurrency, you can use Hashtable directly, but to use hashmap, you need to increase the synchronization process.

In 2.Hashtable, both key and value do not allow null values. In HashMap, NULL can be used as a key with only one key, and one or more keys can have a value of NULL. When the Get () method returns a null value, it can indicate that the key is not in the HashMap, or that the value corresponding to the key is null. Therefore, the get () method cannot be used in HashMap to determine whether a key exists in HashMap and should be judged by the ContainsKey () method.

3. The internal implementation of the two traversal modes is different.

Hashtable and HashMap all use the Iterator. For historical reasons, Hashtable also used the enumeration approach.

4. Using different hash values, Hashtable directly uses the object's hashcode. The hash value is recalculated by the HashMap.

5.Hashtable and hashmap their two internal implementations of the array in the initial size and the way of expansion. The default size of the hash array in Hashtable is 11, and the increment is old*2+1. The default size of the hash array in HashMap is 16, and must be a 2 index.

Java Reading notes---HashMap and hashtable

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.