Which option is better for HashMap?

Which option is better to use:

And why?

And what is loadfactor and modcount in the HashMap property?

When I debug my code in eclipse and look at the HashMap value, it shows a property named loadfactor with a value of 0.75 and a property called modcount with a value of 3.


Where I use hashmap in my code:

I am developing a communication application that you can say chat application. in which I store all sent / received messages in the HashMap. Now that I can’t guess how many messages will be sent / received by the user, I declare a hash map without initial capacity. What I wrote

 Map<String, Map<String, List<String>>> usersMessagesMap = new HashMap<String, Map<String,List<String>>>(); 

if I use it with an initial capacity of 100 or higher, will it work with code?

+4
source share
4 answers

Have you checked the HashMap Javadoc API?

  • capacity - the number of buckets in the hash table
  • The initial capacity is simply the capacity at the time the hash table was created.
  • Load factor - This is an indicator of how full a hash table can be obtained before its capacity is automatically increased.

If the initial size is too high:

Iterating over a collection of views takes time proportional to the "capacity" of the HashMap instance (number of buckets) plus its size (number of displayed key values). Therefore, it is very important not to set the initial power too high (or the load factor too low) if iteration performance is important.

Impact of load factor on productivity:

Typically, the default load factor (.75) offers a good compromise between time and space. Higher values ​​reduce overhead, but increase the cost of the search (reflected in most of the operations of the HashMap class, including get and put). The expected number of entries on the map and its load factor should be adopted when setting its initial throughput in order to minimize the number of rephrasing operations . If the initial capacity is greater than the maximum number of records divided by the load factor, no rehashing operations will ever occur.

Well, in short: depending on the expected size and the expected growth rate, you will need to choose an approximation or vice versa.

Usually, if you know the initial number of elements for your Map, it is recommended to set it during construction, avoiding early retries during initialization.

+8
source

If you know that the set size will be much larger than the initial capacity ( which is 16 ), I would use a higher initial capacity (as the number of keys and the number of N/C increase (where N is the number of stored keys and C is the card capacity ) reaches the load factor, the map array is expanded and the keys are rephrased). In addition, as the size of the map increases exponentially, you will not see a drastic reduction in the number of repeat sessions unless you have a significant number of keys.

So my opinion is this: if you have spare memory and many keys, go for a higher initial capacity.

+4
source
  • Better in terms of simplicity, without initial size.
  • Better in terms of performance, try it yourself.

Found SO stream, Hashmap performance with different bootstrap and load factor

Load factor

The performance of most collisions resolution methods do not depend directly on the number n of stored records, but it strongly depends on the load factor of the table, the n / s ratio between n and the size s of its bucket array. This is sometimes referred to as a fill factor, since it represents part s of buckets in a structure filled with one of n stored records. With a good hash function, the average search cost is almost constant as the load factor increases from 0 to 0.7 (about 2/3 full) or so. - Wikipedia on Load Factor

Now your new question

if I use it with an initial capacity of 100 or higher, will it work with code?

This is not a good idea, you can go with default. Do not think too much about it at the beginning. According to him, "premature optimization is the root of all evil." This would not be of any real use.

+2
source

Strictly speaking, you should not care about the internal fields of the HashMap fields ( loadfactor and modcount ), but not properties: the properties will have getters / setters).

modcount is most likely the number of modifications applied to Map since its inception. He used to detect concurrent modifications and to know when the Iterator becomes “broken” (since the original Map been structurally modified since its inception).

loadfactor is probably a field loadfactor second argument constructor with two arguments . It defines how a “tightly packed” internal array can become until it is modified (which will result in re-mixing of all keys).

+1
source

Source: https://habr.com/ru/post/1338830/


All Articles