Java's Hashtable

Source: Internet
Author: User

First, HashTable

1.1 Basic data structures

First, based on, and combined with code, take a look at the basic data structure of Hashtable:

According to the code, you can see that Hashtable is an array of entry<>, and what is entry? Entry in Hashtable is an inner class of hashtable.class, look at the source code:

Here, we focus on its four fields, hash, key, value, next. The hash here is one of the reasons why it is called Hashtable. This hash value is based on a specific hash algorithm, detailed content can find the corresponding hash algorithm data, here is not introduced. Next, the next field, the next field, is the key and value corresponding to the new hash value that is stored in the linked list when the hash produces duplicates. According to entry's comments it is easy to see that the enrty here is the list element when a hash collision occurs.

1.2 Construction methods

The above is the source of the three construction methods (there is a clone of the construction method). As you can see in, if you do not specify initialcapacity and Loadfactor, the default is 11 and 0.75.

Here, there may be another question, what initialcapacity and Loadfactor are. From the construction method one, it is clear that initialcapacity is the size of the initialized entry[] array, and loadfactor is used to participate in calculating a threshold value. This threshold is used later for Hashtable expansion, and Hashtable expands when the number of all entry in Hashtable is greater than the threshold value. The specific code is given in the following put method.

1.3 Main methods (put && remove)

Put

The above is the main body of the Put method, it is worth noting that the method is limited by the keyword sychronized. As you can see from the For loop, if you have put the same value, the original value will be overwritten.

This section is the AddEntry method, as can be seen from the IF statement, if the count value is greater than the threshold value (the Count value is the value of all entry in Hashtable), then the code for Rehash (), rehash () is given below, Its main work is to expand the original Hashtable, and the original entry the hash. After expansion, the new entry[] size is twice times +1 of the size of the original entry[].

Remove

Logic is the same as put. is to find the corresponding item and then do the operation. Here, the Remove method is still limited by the keyword sychronized.

Java's 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.