In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly shows you "what is the difference between HashMap and Hashtable". The content is simple and clear. I hope it can help you solve your doubts. Let the editor lead you to study and learn this article "what is the difference between HashMap and Hashtable".
The difference between HashMap and Hashtable
1. The main difference between the two is that Hashtable is thread safe, while HashMap is not thread safe.
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 to ensure thread synchronization when calling the method. Of course, the actual operation is still the HashMap instance we passed in. To put it simply, the Collections.synchronizedMap () method helps us to automatically add synchronized to achieve thread synchronization when we operate HashMap, and similar other Collections.synchronizedXX methods have similar principles.)
2.HashMap can use null as key, while Hashtable does not allow null as key
Although HashMap supports null values as key, it is recommended to avoid this use as much as possible, because if you use it carelessly, it will be very troublesome to troubleshoot if it causes some problems.
When HashMap uses null as its key, it is always stored on the first node of the table array
3.HashMap is the implementation of Map interface, and HashTable implements Map interface and Dictionary abstract class.
The initial capacity of 4.HashMap is 16, the initial capacity of Hashtable is 11, and the fill factor of both is 0.75 by default.
When HashMap is expanded, the current capacity is doubled, that is, when capacity*2,Hashtable is expanded, the capacity is doubled + 1, that is, capacity*2+1.
5. The two methods of calculating hash are different.
The calculation of hash by Hashtable directly uses the 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.
The underlying implementation of 6.HashMap and Hashtable is array + linked list structure.
The difference between HashSet, HashMap and Hashtable
In addition to HashMap and Hashtable, there is also a hash collection HashSet. The difference is that HashSet is not a key value structure, but only stores non-repetitive elements, which is equivalent to a simplified version of HashMap, but only contains the key in HashMap.
This is also confirmed by looking at the source code. HashSet is implemented internally using HashMap, but all the value of the Ha11shMap in HashSet is the same Object, so HashSet is also non-thread safe. As for the difference between HashSet and Hashtable, HashSet is a simplified HashMap, so you know.
Here are the implementations of several main methods of HashSet
The implementation principle of HashMap and Hashtable
When adding, deleting, and getting elements, hash is calculated first, and index, that is, the subscript of the table array, is calculated according to hash and table.length, and then the corresponding operations are performed. Here is a simple implementation of HashMap as an example.
The creation of HashMap
When HashMap initializes by default, it creates an Entry array with a default capacity of 16, a default load factor of 0.75, and a critical value of 16 to 0.75.
Put method
HashMap does special handling for null key, which is always placed in the table [0] position
The put process calculates hash first, then calculates the index value through hash and table.length, and then places key in the table [index] position. When other elements already exist in table [index], a linked list is formed in the table [index] position, the newly added elements are placed in table [index], and the original elements are linked through Entry's next, so as to solve the hash conflict problem in the form of a linked list, when the number of elements reaches the critical value (capactiy*factor). Then the capacity is expanded so that the length of the table array becomes table.length*2.
Get method
Also, when key is null, special processing will be performed to find the element whose key is null on the linked list of table [0].
The process of get is to first calculate hash, then calculate the index value by touching hash and table.length, and then traverse the linked list on table [index] until you find key, and then return
Remove method
The remove method is similar to put get in that it calculates hash, calculates index, and then traverses the lookup to remove the found elements from the table [index] list
Resize method
The resize method is not disclosed in hashmap, which implements a very important hashmap expansion. The specific process is: first create a new table with the capacity of table.length*2, modify the critical value, then calculate the hash value of the elements in the table and recalculate the index into the new table using hash and table.length*2.
It should be noted here that the index is recalculated with all the hash of each element, rather than simply moving the original table corresponding index location element to the new table corresponding position.
Clear () method
The clear method is very simple: iterate through the table and set each location to null, while changing the number of elements to 0
It is important to note that the clear method only knows the elements inside and does not reset the capactiy.
ContainsKey and containsValue
The containsKey method first calculates the hash, then uses hash and table.length to get the index value, and traverses the table element to find out whether it contains the same value as key.
The containsValue method is more crude, that is, it traverses all the elements directly until it finds value, so it can be seen that HashMap's containsValue method is essentially no different from ordinary arrays and list's contains method. You can't expect it to be as efficient as containsKey.
Hash and indexFor
The hash method is to hash the hashcode twice to get a better hash value.
To better understand this, we can simplify these two methods to int index= key.hashCode () / table.length, which can be replaced by taking the method in put as an example
The above is all the content of the article "what's the difference between HashMap and Hashtable". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.