In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
What is the difference between HashMap and HashTable? I believe many inexperienced people don't know what to do about it. Therefore, this article summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.
1. Thread safety: HashMap is non-thread safe, HashTable is thread safe; the methods inside HashTable are basically modified by synchronized. Use ConcurrentHashMap if you want to ensure thread safety. Efficiency: because of thread safety issues, HashMap is a little more efficient than HashTable. In addition, HashTable is basically eliminated, do not use it in the code; 3, support for Null key and Null value: HashMap, null can be used as a key, there is only one such key, there can be one or more keys corresponding to the value of null. . However, in HashTable, as long as the key value of put input has a null, it directly throws NullPointerException.
4. The difference between the initial capacity and each expansion capacity:
If the initial capacity value is not specified when ① is created, the default initial size of Hashtable is 11, and then the capacity becomes the original 2n+1 for each expansion. The default initialization size for HashMap is 16. After each expansion, the capacity is doubled. If the initial capacity value is given when ② is created, Hashtable will directly use the size you give, and HashMap will expand it to the power of 2 (guaranteed by the tableSizeFor () method in HashMap, the source code is given below). That is to say, HashMap always uses the power of 2 as the size of the hash table, which will be explained later. 5. Underlying data structure: the HashMap after JDK1.8 has changed greatly in solving hash conflicts. When the length of the linked list is greater than the threshold (default is 8), the linked list will be converted into a red-black tree to reduce the search time. Hashtable does not have such a mechanism. Constructor with initial capacity in HashMap: public HashMap (int initialCapacity, float loadFactor) {if (initialCapacity)
< 0) throw new IllegalArgumentException("Illegal initial capacity: " + initialCapacity); if (initialCapacity >MAXIMUM_CAPACITY) initialCapacity = MAXIMUM_CAPACITY; if (loadFactor > > 1; n | = n > 2; n | = n > 4; n | = n > 8; n | = n > 16; return (n
< 0) ? 1 : (n >= MAXIMUM_CAPACITY)? MAXIMUM_CAPACITY: n + 1;} after reading the above, have you mastered the difference between HashMap and HashTable? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!
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.