Related to HashMap OR HashTable 2004-07-27 - By Manish Malhotra
Back Hi, I know its not purely J2EE issue but, I have a doubt regarding the Map's func. in Java.
HashMap have one constructor as HashMap(int cap, float loadFactor) which says that create a HashMap with this much capacity and this loadFactor. And its threshold value is (cap*loadFactor) after which it will reHash the Map.
But actually Map's const code is like this : public HashMap(int initialCapacity, float loadFactor) { if (initialCapacity < 0) throw new IllegalArgumentException("Illegal initial capacity: " + initialCapacity); if (initialCapacity > MAXIMUM_CAPACITY) initialCapacity = MAXIMUM_CAPACITY; if (loadFactor <= 0 || Float.isNaN(loadFactor)) throw new IllegalArgumentException("Illegal load factor: " + loadFactor);
// Find a power of 2 >= initialCapacity int capacity = 1; while (capacity < initialCapacity) capacity <<= 1;
this.loadFactor = loadFactor; threshold = (int)(capacity * loadFactor); table = new Entry[capacity]; init(); }
So its making capacity as power of 2.
And put method resize only if there is data more then the threshold is try to add.
Now if I Create HashMap(1, 2.0f) const.
Its capacity would be cap = 1*2 = 2
Its threshold would be th = 2.0 * 2 = 4;
And the size of the array of Entry would be 2. (Internally HashMap uses Entry to maintain the objects key, value)
Hence, If I add more then 2 entries in this HashMap then how it worked because before the threeshold comes it wont resize and the inti array of Entry class is size of 2, So third should give exception ...... I know its handeled but I dnt kn why?
Its interesting.... So please explain.......
regards, Manish
=========================================================================== To unsubscribe, send email to listserv@(protected) and include in the body of the message "signoff J2EE-INTEREST". For general help, send email to listserv@(protected) and include in the body of the message "help".
|
|