Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

What is the difference between newHashMap () and Maps.newHashMap ()

Shulou Source: shulou.com Published: 2022-06-02 10:30:23 09月25日 Update

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.

Tags: Security capacity source code problem number code element writing manual time help neat effective clear that is interest memory content at the same time instance Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL Linux Xiaomi MariaDB Shulou Information