Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What's the difference between HashMap and Hashtable?

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

HashMap and Hashtable are different, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.

Thread safety

The main difference between the two is that Hashtable is thread-safe, while HashMap is not. Synchronized keyword is added to the implementation method of Hashtable to ensure thread synchronization, so the performance of HashMap is relatively higher. If we usually use it without special needs, we recommend using HashMap. If we use HashMap in a multithreaded environment, we need to use the Collections.synchronizedMap () method to obtain a thread-safe collection (Collections.synchronizedMap () implementation principle is that Collections defines an inner class of SynchronizedMap, which implements the Map interface. Use synchronized when calling methods to ensure thread synchronization, of course, the actual operation is still our incoming HashMap instance, to put it simply, the Collections.synchronizedMap () method helps us to automatically add synchronized to achieve thread synchronization when operating HashMap, similar to other Collections.synchronizedXX methods with similar principles.

NULL value

HashMap can use null as a key, but it is recommended to avoid it as much as possible. When HashMap uses null as its key, it is always stored on the first node of the table array. When the get () method returns a null value, it can either indicate that the key does not exist in HashMap or that the value corresponding to the key is null. Therefore, the get () method cannot be used to determine whether a key exists in the HashMap in HashMap, but should be determined by the containsKey () method. Hashtable does not allow null as a key.

Inheritance relationship

HashMap inherits AbstractMap,HashTable inherits the Dictionary abstract class, and both implement the Map interface.

Initial value of capacity

The initial capacity of HashMap is 16 and the initial capacity of Hashtable is 11, and the fill factor of both is 0.75 by default.

Expansion mode

When HashMap is expanded, the current capacity is doubled, that is, when capacity*2,Hashtable is expanded, the capacity is doubled by + 1, that is, capacity*2+1.

Underlying structure

The underlying implementation of HashMap and Hashtable is an array + linked list structure.

Hash algorithm

Hashtable calculates hash by directly using hashcode of key to directly model the length of the table array:

Int hash = key.hashCode ()

Int index = (hash & 0x7FFFFFFF)% tab.length

HashMap calculates hash to hash the hashcode of key twice to get a better hash value, and then takes the length of the table array:

Static int hash (int h) {

H ^ = (h > 20) ^ (h > 12)

Return h ^ (h > 7) ^ (h > 4)

}

Static int indexFor (int h, int length) {

Return h & (length-1)

}

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report