java foundation to improve the Hashtable

        Last article we discussed the HashMap , and Hashtable and HashMap also mentioned very similar, except outside the Hashtable is synchronized and does not allow null values. Now, build on the progress, Hashtable start learning.

Brief introduction

        The old rules, to go over the comments.

  1. Any non-null object can be used as key or value into them.

  2. Into one of the key must implement the hashCode and equals method.

  3. HashMap with the same, there are two parameters that affect performance Hashtable, the initial capacity of the initial capacity and the load factor load factor. Their role in the HashMap are the same, detailed look at this article

  4. If you want to store a lot of Hashtable key-value pairs, then at the beginning of the big points he set the initial capacity is better.

  5. Hashtable that is returned by the iterator Iterator are based on fail-fast mechanism fail-fast of.

  6. Unlike the new collection implementations, Hashtable is synchronized, without the need to consider thread-safe, it is recommended to use alternative Hashtable HashMap for better performance. If you need a highly synchronous thread-safe, it is recommended to use ConcurrentHashMap instead of Hashtable. (Feeling can be deleted from the Hashtable Jdk, the grandmother hurt grandmother does not love: joy :).

Storage structure

First look at its properties:

In fact, these see that, but we still talk about one.

  1. table: This is the place to store key-value pairs, and one will we illustrate it.
  2. The total number of stored key-value pairs: count
  3. threshold: When the number of key-value pairs Hashtable exceeds this value will be the expansion operation, this value is equal to the product of capacity and load factor
  4. loadFactor: load factor, representing the full scale of this Hashtable permissible. This container can store up to what percentage can be stored in this container capacity.
  5. modCount: fail-fast mechanism to use, not repeat them.

In fact, Hashtable storage structure is an array + form a linked list, we illustrate this:

In fact, the same can be seen with the storage structure of the tree in front of the HashMap not.

Comparing binding HashMap

Start with the constructor looks

In the last article , we said, we did not actually directly initialCapacity parameters passed in a HashMap, but the use of a method to convert it into the nearest power of n from its number 2, but not so Hashtable can be seen in the code, initialCapacity used directly in the constructor of the table are initialized. And randomly calculated the threshold value threshold.

We have repeatedly mentioned Hashtable is synchronized, and that this is reflected in sync where is it? It is reflected in the following:

See out of it? In fact reflected in these methods preceded by the keyword synchronized.

Expansion mechanism

We look at direct expansion mechanism, not to look at those who put in a similar method. We look rehash source:

The same source, the table size is determined to go to the array of the new capacity under the old capacity. We note that the new capacity is not direct but is twice the old capacity of the old capacity of a two doubly

Secondly, we may note, Hashtable used a for loop for each element of which is recalculated its index in accordance with its new array of Hash.

to sum up

  1. When used with ConcurrentHashMap HashMap improve performance, we need to consider thread safety is not required to consider thread safety

  2. Synchronization is reflected in the use of the synchronized keyword for each method.

  3. Storage structure is an array list +

  4. The default expansion when expansion is twice the original plus one

  5. During the elements for each element of the old copy stored in the array recalculated the index when the expansion and inserted into the new array.

Reproduced in: https: //juejin.im/post/5cf26334e51d455070226f3d

Guess you like

Origin blog.csdn.net/weixin_33694172/article/details/91478549