Harry Joy
Harry Joy

Reputation: 59650

Which is better option for HashMap?

Which is better option to use:

And why?

Also what is loadfactor and modcount in HashMap's property?

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


Where i use hashmap in my code:-

I'm developing a communication application you can a say a chat application. in which i store all send/received messages in HashMap. Now as i cant assume how many messages will a user will send/receive i declare a hashmap without initial capacity. What i have written is

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

if i use it with initial capacity of 100 or higher will it effect the code?

Upvotes: 4

Views: 3836

Answers (4)

Adeel Ansari
Adeel Ansari

Reputation: 39887

  • Better in terms of simplicity, without initial size.
  • Better in terms of performance, try that out yourself.

Found an SO thread, Performance of Hashmap with Different Initial Capacity And Load Factor

Load Factor

The performance of most collision resolution methods does not depend directly on the number n of stored entries, but depends strongly on the table's load factor, the ratio n/s between n and the size s of its bucket array. Sometimes this is referred to as the fill factor, as it represents the portion of the s buckets in the structure that are filled with one of the n stored entries. With a good hash function, the average lookup cost is nearly constant as the load factor increases from 0 up to 0.7(about 2/3 full) or so. -- Wikipedia on Load Factor

Now your new question

if i use it with initial capacity of 100 or higher will it effect the code?

Its not a good idea, you are good to go with default thing. Don't think too much about this in the start. As he said, "premature optimisation is the root of all evil". It wouldn't give any real benefit, whatsoever.

Upvotes: 2

Tomas Narros
Tomas Narros

Reputation: 13468

Have you checked the HashMap API Javadoc?

  • The capacity is the number of buckets in the hash table
  • The initial capacity is simply the capacity at the time the hash table is created
  • The load factor is a measure of how full the hash table is allowed to get before its capacity is automatically increased

On setting the initial size too high:

Iteration over collection views requires time proportional to the "capacity" of the HashMap instance (the number of buckets) plus its size (the number of key-value mappings). Thus, it's very important not to set the initial capacity too high (or the load factor too low) if iteration performance is important.

Impact of the load factor on the performance:

As a general rule, the default load factor (.75) offers a good tradeoff between time and space costs. Higher values decrease the space overhead but increase the lookup cost (reflected in most of the operations of the HashMap class, including get and put). The expected number of entries in the map and its load factor should be taken into account when setting its initial capacity, so as to minimize the number of rehash operations. If the initial capacity is greater than the maximum number of entries divided by the load factor, no rehash operations will ever occur.

Well, in short: depending on the estimated size and the expected growth ratio, you'll have to chose an aproximation or the opposite.

Usually, if you know the initial number of elements for your Map, it's recommended to set it at building time, avoiding early rehashes on initialization time.

Upvotes: 8

PaoloVictor
PaoloVictor

Reputation: 1306

If you know that the keyset size will be much bigger than the initial capacity (which is 16), I'd use a higher initial capacity to reduce rehashing (as the number of keys grow and the N/C value (where N is the number of stored keys and C is the map's capacity) reaches the load factor, the map array is extended and the keys are rehashed). Also, since the map size increases exponentially, you won't see a drastic reduction on the number on rehashing unless you have a significant number of keys.

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

Upvotes: 4

Joachim Sauer
Joachim Sauer

Reputation: 308001

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

The modcount is most likely the number of modifications applied to the Map since its creation. It's used to detect concurrent modifications and to know when an Iterator becomes "broken" (because the originating Map was structurally modified since it was created).

The loadfactor is probably a field storing the second argument of the two-argument constructor. It defines how "tightly packed" the internal array may become until it is resized (which results in a re-hashing of all the keys).

Upvotes: 1

Related Questions