In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)05/31 Report--
This article introduces the relevant knowledge of "what are the similarities and differences between HashMap and HashTable". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Similarities: 1 > are subclasses of Map. 2 > is based on the Entry array. Difference: 1 > HashMap multithreading is not safe, HashTable is thread safe. 2 > key and value of HashMap both allow key and value of null,HashTable are not allowed to be null (null pointer exception is thrown when key or value is null). 3 > the default capacity of HashMap is 16, and the capacity after expansion is twice as much as before; the default capacity of HashTable is 11, and the capacity after expansion is 2 times + 1. 4 > the way to get bucket is different:-HashTable gets array subscript: modular code: int hash = hash (key) Int index = (hash & 0x7FFFFFFF)% tab.length Note: 1) get a hashValue according to key [Note: hashValue=hash (key) & 0x7FFFFFFF], and then use hashValue to get the subscript of the array, that is, hashValue%length 2) Modularization can basically ensure that the element hash in the hash is relatively uniform, but the modularization will be used to divide the operation, the efficiency is very low. -how HashMap gets array subscripts: bitwise and code: int hash = hash (key); int I = indexFor (hash, table.length) Static int indexFor (int h, int length) {return h & (length-1) } description: 1) get a hashValue according to key [Note: hashValue=hash (key)], and then use hashValue to sum the length-1 to get the subscript of the array, that is, the length of the hashValue& (length-1) 2) array must be an integer power of 2. The reasons are as follows: first, if length is an integer power of 2, then hashValue& (length-1) is equivalent to hashValue%length, then hashValue& (length-1) also implements uniform hashing, but (bit operation) is more efficient. 1 > Induction: 2 ^ 1-1 = 0000 0001 2 ^ 2-1 = 0000 0011 2 ^ 3-1 = 0000 0111 2 ^ n- 1 = 0000 (n 1) 2 > example: if: hashValue=29 Length=16: hashValue & (length-1) = = > 29 & (2 ^ 4-1) = = > 00011101 & 00001111 = 00001101 = > 13 = > 013 = > 0
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.