最近看一Hashtable的源代码,不懂。它的构造函数如下:Hashtable() 
          Constructs a new, empty hashtable with a default initial capacity (11) and load factor, which is 0.75. Hashtable(int initialCapacity) 
          Constructs a new, empty hashtable with the specified initial capacity and default load factor, which is 0.75. Hashtable(int initialCapacity, float loadFactor) 
          Constructs a new, empty hashtable with the specified initial capacity and the specified load factor. 想问的是,整数initialCapacity表示容量(默认11),那浮点数loadFactor是什么呢(默认0.75)?
而且,为什么整数要默认11,浮点要默认0.75???百思不解,而又没发现象我这样弱智的问题,所以请赐教。

解决方案 »

  1.   

    整数initialCapacity表示容量(默认11),
    也就是初始化时buckets的数量,这个很好理解;
    那浮点数loadFactor是什么呢(默认0.75),
    它表示Hashtable所允许的饱和程度
    (饱和程度过大的时候容易发生hash collision);
    0.75表示Hashtable 75%的容量被装满后,
    Hashtable的容量就会自动增加。英文版的更纯正些:
    The load factor is a measure of how full the hash table is allowed to get before its capacity is automatically increased. When the number of entries in the hashtable exceeds the product of the load factor and the current capacity, the capacity is increased by calling the rehash method.
      

  2.   

    to  dext(德克斯特) :容量11 指的是key的字节长度 or value的字节长度??不会是存入的key/value的对数限制吧?而装填因子又用来做什么的呢?总要应用在一个计算式子中吧!是什么呢??
      

  3.   

    to  mituzhishi(向往未来) :恕我愚笨,buckets是什么?
      

  4.   

    也就是说hashtable是个动态的存储空间,
    但是为什么不等到用完了再扩充呢?冲突是说 出现相同的key值吗?那只要通过程序自动比较避免出现相同的key值就好了。
    或者冲突是其他的意思??
      

  5.   

    谢谢各位关心,目前的理解就是:11 是个初始容量值。就是一开始可以装的 key/value 对数。0.75 是个装载因素,就是添到75%,hashtable 自动增加,增加到 原来容量*2+1。 因为hashtable 不是按顺序存储的,所以每装载个 key/value,都要去计算查找空闲的位置,因此,这个值如果设大了(当然不能大于1),装的越多,查找所用的时间就越多。这个值要设小点呢?当然,查找空闲空间的时间是小了,但浪费的空间就大了。总之根据实际用途来设置,以达到适合你的最好效果。