Why ArrayList grows at a rate of 1.5, but for Hashmap it's 2?

后端 未结 7 1129
暖寄归人
暖寄归人 2020-12-28 17:28

As per Sun Java Implementation, during expansion, ArrayList grows to 3/2 it\'s initial capacity whereas for HashMap the expansion rate is double. What is reason behind this?

7条回答
  •  被撕碎了的回忆
    2020-12-28 17:52

    The accepted answer is not actually giving exact response to the question, but comment from @user837703 to that answer is clearly explaining why HashMap grows by power of two.

    I found this article, which explains it in detail http://coding-geek.com/how-does-a-hashmap-work-in-java/

    Let me post fragment of it, which gives detailed answer to the question:

    // the function that returns the index of the bucket from the rehashed hash
    static int indexFor(int h, int length) {
        return h & (length-1);
    }
    

    In order to work efficiently, the size of the inner array needs to be a power of 2, let’s see why.

    Imagine the array size is 17, the mask value is going to be 16 (size -1). The binary representation of 16 is 0…010000, so for any hash value H the index generated with the bitwise formula “H AND 16” is going to be either 16 or 0. This means that the array of size 17 will only be used for 2 buckets: the one at index 0 and the one at index 16, not very efficient…

    But, if you now take a size that is a power of 2 like 16, the bitwise index formula is “H AND 15”. The binary representation of 15 is 0…001111 so the index formula can output values from 0 to 15 and the array of size 16 is fully used. For example:

    • if H = 952 , its binary representation is 0..01110111000, the associated index is 0…01000 = 8
    • if H = 1576 its binary representation is 0..011000101000, the associated index is 0…01000 = 8
    • if H = 12356146, its binary representation is 0..0101111001000101000110010, the associated index is 0…00010 = 2
    • if H = 59843, its binary representation is 0..01110100111000011, the associated index is 0…00011 = 3

    This is why the array size is a power of two. This mechanism is transparent for the developer: if he chooses a HashMap with a size of 37, the Map will automatically choose the next power of 2 after 37 (64) for the size of its inner array.

提交回复
热议问题