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 > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
NewHashMap () and Maps.newHashMap () what is the difference, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can gain something.
1, new HashMap () this is java native API writing, you need to manually add generics. There is a thread safety problem, which will occur when you expand the capacity to calculate hash. In the rehash method, you can take a look at the source code if you are interested.
Map result = new HashMap ()
2. Maps.newHashMap (), which is written by google's guava.jar to simplify the code and does not require you to write generics manually. It's convenient, the code looks neat, and there are security problems, because it is essentially a HashMap () returned to you, so the security aspect is the same as HashMap.
Map result = Maps.newHashMap ()
NewHashMap () source code:
/ * Creates a mutable, empty {@ code HashMap} instance. * *
Note: if mutability is not required, use {@ link * ImmutableMap#of ()} instead. * *
Note: if {@ code K} is an {@ code enum} type, use {@ link * # newEnumMap} instead. * @ return a new, empty {@ code HashMap} * / public static HashMap newHashMap () {return newHashMap ();}
3. Maps.newHashMapWithExpectedSize (10) when creating an instance, you need to set the default number of elements.
Source code analysis:
We use expectedSize + expectedSize / 3 to calculate 10: 10 *
When the capacity of the hash table maintained internally by HashMap reaches 75% (by default), rehash will be triggered, and the process of rehash is time-consuming. Therefore, if the initialization capacity should be set to expectedSize + expectedSize / 3, it can effectively reduce conflicts and errors.
So, I can think that setting the default capacity to expectedSize + expectedSize / 3 is a relatively good performance option when we know exactly the number of elements in HashMap, but it also sacrifices some memory.
Public static HashMap newHashMapWithExpectedSize (int expectedSize) {return newHashMap (capacity (expectedSize));} / * * Returns a capacity that is sufficient to keep the map from being resized as * long as it grows no larger than expectedSize and the load factor is > = its * default. * / static int capacity (int expectedSize) {if (expectedSize < 3) {checkNonnegative (expectedSize, "expectedSize"); return expectedSize + 1;} if (expectedSize < Ints.MAX_POWER_OF_TWO) {return expectedSize + expectedSize / 3;} return Integer.MAX_VALUE; / / any large value} 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.
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.