Mano Pragadeeshwar wrote:Thank you Mike. I declared the size of the Map since I know for sure that is the number of elements.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Winston Gutkowski wrote:
Mano Pragadeeshwar wrote:Thank you Mike. I declared the size of the Map since I know for sure that is the number of elements.
That's almost always the best way to go, since it makes loading the Map optimally fast.
Winston Gutkowski wrote:However, a HashMap with 2 elements strikes me as possibly over-engineering.
![]()
Mike Simmons wrote:Well, thing is, if you put 2 entries into such a map, it's going to resize to capacity 4, because the default load factor is .75.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Mike Simmons wrote:Well, capacity isn't exactly the same as the number of elements a hash table can hold, anyway. You could put 100 elements in a table with size 2 for example - it's just that you would have ~50 collisions in each bucket. And you can have collisions even when the size is less than the capacity. That's why they pick 0.75 as a default. No value would really guarantee no collisions - they just try to pick a reasonable value there.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Mike Simmons wrote:
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
Rob Spoor wrote:I hate that code. You're creating an anonymous inner class only to automatically add some entries.
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
Mike Simmons wrote:Winston, you might appreciate this. On JavaPosse's latest podcast, Tor just discovered the same thing we were talking about, that using HashSet's constructor as if you're telling it the number of elements do expect does not avoid resizing. They rant on it a bit, agreeing with you about what they expected it should do. It's in the Aug 10 podcast, around 0:59-1:07. Bottom line: use Guava, which does what you want here.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Did you see how Paul cut 87% off of his electric heat bill with 82 watts of micro heaters? |