In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly shows you "What are the differences between HashMap, HashSet and HashTable in Java". The content is simple and easy to understand and organized clearly. I hope it can help you solve your doubts. Let Xiaobian lead you to study and learn "What are the differences between HashMap, HashSet and HashTable in Java".
Differences between HashMap, HashSet and HashTable: different inherited parent classes, different thread safety, whether to provide the contains method, whether to allow null values for **key and value, different calculation methods for hash values, different initialization and expansion methods for arrays used in internal implementation.
Difference 1: Inherited parent classes are different
Hashtable inherits from the Dictionary class, while HashMap inherits from the AbstractMap class. But both implement the Map interface.
Difference 2: thread security is different
Methods in Hashtable are Synchronized, while methods in HashMap are non-Synchronized by default.
Difference 3: Whether to provide the contains method
HashMap removes the contents method of Hashtable and changes it to containsValue and containsKey, because the contents method is easy to misunderstand.
Hashtable retains three methods: contains, containsValue and containsKey, where contains and containsValue have the same function.
Difference 4: **key and value whether null value is allowed (interview prefer to ask)
Both key and value are objects and cannot contain duplicate keys, but can contain duplicate values.
In Hashtable, null values are not allowed for key and value.
In HashMap, null can be used as a key, and there can be only one such key; one or more keys can correspond to null. When the get() method returns null, it may be because the key does not exist in the HashMap, or it may make the value corresponding to the key null. Therefore, in HashMap, it is not possible to determine whether a key exists in HashMap by the get() method, but should be determined by the containsKey() method.
Difference 5: The hash value is calculated differently
Hashtable directly uses the hashCode of the object, while HashMap makes some changes on the basis of the hashCode of the object.
Difference 6: The array initialization and expansion methods used by the internal implementation are different.
The initial memory size is different, HashTable initial size is 11, and HashMap initial size is 16
The above is "Java HashMap, HashSet, HashTable What are the differences" All the contents of this article, thank you for reading! I believe that everyone has a certain understanding, hope to share the content to help everyone, if you still want to learn more knowledge, welcome to pay attention to 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.