"The load factor is a measure of how full the hash table is allowed to get before its capacity is automatically increased. " Is the above statement means that until i put 8 entries into the collection, the collection won't increase automatically if we take default initial capacity value=10 and default load factor value=0.75 into consideration?

"When the number of entries in the hash table exceeds the product of the load factor and the current capacity, the hash table is rehashed (that is, internal data structures are rebuilt) so that the hash table has approximately twice the number of buckets." Does it mean that will my hash table gets rehashed when i have 8 entries in the hash table if the initial capacity is 10 and load factor is 0.75 of my hash table?

"If the initial capacity is greater than the maximum number of entries divided by the load factor, no rehash operations will ever occur." If i need to store a maximum of 50 entries into the collection, then initial capacity=50/0.75=66.6 based on the preceding formula. So will it be a good idea to keep initial capacity of my collection to 70? If yes, should i need to change the load factor or keep it to same value of 0.75? Also under what circumstances should we need to increase the load factor value?

divya chowdhary wrote:In the java docs it was said that

"The load factor is a measure of how full the hash table is allowed to get before its capacity is automatically increased. " Is the above statement means that until i put 8 entries into the collection, the collection won't increase automatically if we take default initial capacity value=10 and default load factor value=0.75 into consideration?

"When the number of entries in the hash table exceeds the product of the load factor and the current capacity, the hash table is rehashed (that is, internal data structures are rebuilt) so that the hash table has approximately twice the number of buckets." Does it mean that will my hash table gets rehashed when i have 8 entries in the hash table if the initial capacity is 10 and load factor is 0.75 of my hash table?

Both of those are pretty much saying the same thing, and, yes, your interpretation is correct.

"If the initial capacity is greater than the maximum number of entries divided by the load factor, no rehash operations will ever occur." If i need to store a maximum of 50 entries into the collection, then initial capacity=50/0.75=66.6 based on the preceding formula. So will it be a good idea to keep initial capacity of my collection to 70? If yes, should i need to change the load factor or keep it to same value of 0.75? Also under what circumstances should we need to increase the load factor value?

I don't think I've ever messed with those parameters. Rehashing isn't something we usually even notice. About the only time I would expect you'd bother with tweaking them is if you have a performance or memory problem, and you've profiled and determined that rehashing a particular map is the source of that problem.

divya chowdhary wrote:"The load factor is a measure of how full the hash table is allowed to get before its capacity is automatically increased. " Is the above statement means that until i put 8 entries into the collection, the collection won't increase automatically if we take default initial capacity value=10 and default load factor value=0.75 into consideration?

Yup.

"When the number of entries in the hash table exceeds the product of the load factor and the current capacity, the hash table is rehashed (that is, internal data structures are rebuilt) so that the hash table has approximately twice the number of buckets." Does it mean that will my hash table gets rehashed when i have 8 entries in the hash table if the initial capacity is 10 and load factor is 0.75 of my hash table?

Yup. It's pretty much a re-statement of the first point.

"If the initial capacity is greater than the maximum number of entries divided by the load factor, no rehash operations will ever occur." If i need to store a maximum of 50 entries into the collection, then initial capacity=50/0.75=66.6 based on the preceding formula. So will it be a good idea to keep initial capacity of my collection to 70?

Or 67, if you know that you only want 50.

If yes, should i need to change the load factor

No.

or keep it to same value of 0.75?

Unless you have a very good reason not to (and to do that, you'd probably need to know something about what you're going to be storing).

Also under what circumstances should we need to increase the load factor value?

If space is at a premium you might want to raise it; if speed is absolutely critical and you suspect that the things you are storing might cause collisions (though how you'd know that I'm not quite sure), you could lower it, but I've never done it myself in 12 years of writing Java, and it will make the map bigger.

Winston

Bats fly at night, 'cause they aren't we. And if we tried, we'd hit a tree -- Ogden Nash (or should've been).
Articles by Winston can be found here

Just one complication about that: HashMap restricts itself as to how much its capacity is: 16, 32, 64, 128, 256, etc. You can restrict the capacity to 64 by using a load factor of 80% (64 × 0.8 = 51.2), but Winston is quite right. Forget about the microsecond it will take to rehash the Map. Or tell it you want to put 67 things in and you will get a 128 capacity. Remember when HashMap’s older counterpart was written, the cost of memory was maybe 1000000 times what it costs now.

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 45364

42

posted

0

But in the days when they might have worried about memory, the API documentation said little about the load factor.

divya chowdhary
Greenhorn

Joined: Nov 03, 2012
Posts: 10

posted

0

Thanks to one and all for giving your valuable answers... Got clarified...