top button
Flag Notify
    Connect to us
      Facebook Login
      Site Registration Why to Join

Facebook Login
Site Registration

Java: How HashMap loadfactor and threshold values defined? Please explain with an example?

+1 vote
121 views
Java: How HashMap loadfactor and threshold values defined? Please explain with an example?
posted Jul 27, 2015 by Samardeep Acharya

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+1 vote

See this code lines:

HashMap<String, Integer> mapJdK = new HashMap<String, Integer>();

suppose the default load factor is 0.75 and the default initial capacity of a hash map is 16. This means that the threshold for the next resize equates like:

DEFAULT_INITIAL_CAPACITY = 16; 
DEFAULT_LOAD_FACTOR = 0.75; 
THRESHOLD = DEFAULT_INITIAL_CAPACITY * DEFAULT_LOAD_FACTOR;

The hash map will automatically rehash the contents of the hash map into a new array with a larger capacity. This happens when the number of data in the hash map exceeds the THRESHOLD. Each resize leads to a doubled capacity 16*0.75, 32*0.75, 64*0.75, etc.Like
The minimal initial capacity would be (number of data)/0.75+1 then java hashmap with reasonable values:

int capacity = (int) ((expected_maximal_number_of_data)/0.75+1);
HashMap<String, Integer> mapJdK = new HashMap<String, Integer>(capacity);
answer Mar 22, 2016 by Shivam Kumar Pandey
Contact Us
+91 9880187415
sales@queryhome.net
support@queryhome.net
#280, 3rd floor, 5th Main
6th Sector, HSR Layout
Bangalore-560102
Karnataka INDIA.
QUERY HOME
...