The similarities and differences between HashMap and Hashtable (detailed comparison), hashmaphashtable

Source: Internet
Author: User

The similarities and differences between HashMap and Hashtable (detailed comparison), hashmaphashtable
I. Summary

Can we directly judge whether two objects are equal based on the hashcode value? Certainly not, because different objects may generate the same hashcode value. Although it is not possible to judge whether two objects are equal based on the hashcode value, two objects can be determined based on the hashcode value. If the hashcode values of two objects are different, they must be two different objects. To determine whether two objects are truly equal, you must use the equals method.

That is to say, for two objects, if the result obtained by calling the equals method is true, the hashcode values of the two objects must be equal. If the equals method returns false, the hashcode values of the two objects are not necessarily different. If the hashcode values of the two objects are not the same, the equals method returns false. If the hashcode values of the two objects are equal, the result obtained by the equals method is unknown.

HashMap and Hashtable do not guarantee the order of map, and do not guarantee that the order will not change with time.

The HashMap instance has two parameters that affect performance: initial capacity and load factor. Capacity is the number of buckets in hashtable. The initial capacity is the capacity when hashtable is created. The load factor can automatically increase capacity when hashtable is full. When the number of entries in hashtable exceeds the product of load factor and current capacity, hashtable will re-Hash (meaning that the internal data structure is rebuilt), so hashtable has about twice the number of buckets.

As a general rule, the default load factor (0.75) provides a good balance between time and space consumption. The larger the value, the smaller the space overhead, but the higher the traversal cost (as shown in most operations, including get and put ). When the initial capacity is set, the number of map entries and load factor should be considered to minimize the number of re-hash operations. If the initial capacity is greater than the maximum number of entries divided by the load factor, the re-hash operation will not happen. However, setting the initial capacity is too wasteful.

If many mapping instances are stored in HashMap instances, using a sufficiently large capacity during creation will allow mapping to be stored more efficiently, because it will not re-hash as the number of tables increases. Note that using many keys with the same hashCode () will definitely reduce the performance of any hashtable.

Ii. Similarities

DEFAULT_LOAD_FACTOR

0.75

TREEIFY_THRESHOLD

8

UNTREEIFY_THRESHOLD

6

MIN_TREEIFY_CAPACITY

Otherwise, resize ()

64

Size

Mapping quantity

Threhold

Capacity * load factor

Iii. Differences

 

HashMap

Hashtable

Thread Security

Insecure

Security

Keys and values that allow null

Allow

Not Allowed

Implementation and inheritance

Implement Map

Implement Map and inherit Dictionary

Traversal Mode

Iterator

Iterator and Enumeration

Calculate the hash value

(Key = null )? 0: (h = key. hashCode () ^ (h >>> 16)

(Key. hashCode () & 0x7FFFFFFF)

Calculate array subscript

(Length-1) & hash

Hash % length

DEFAULT_INITIAL_CAPACITY

16

11

Capacity increase mode

Old * 2

The length is always a power of 2.

Old * 2 + 1

Constructor

Threshold = tableSizeFor (initialCapacity)

Threhold = initialCapacity * load factor

Resize

From 0-cap

The chain table sequence remains unchanged.

From cap-0

The chain table is in the opposite order.

 

Calculate the array subscript: When length is always the Npower of 2, h & (length-1) is equivalent to the modulo of length, that is, h % length, however, "&" is more efficient than "%.

Capacity increase mode: When the array length is 2 to n power, the probability of having the same index calculated by different keys is small, so the data distribution on the array is relatively even, that is to say, the chance of collision is small. Relatively, you do not need to traverse the linked list at a certain position during the query, so the query efficiency is high. As a result, different hashmaps of resize () directly use the previous array table, and Hashtable needs to be re-computed.

--- [Transfer]

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.