`

hashtable arraylist 容量

    博客分类:
  • Java
 
阅读更多

  Each ArrayList instance has a capacity. The capacity is the size of the array used to store the elements in the list. It is always at least as large as the list size. As elements are added an ArrayList, its capacity grows automatically. The details of the growth policy are not specified beyond the fact that adding an element has constant amortized time cost.
   An application can increase the capacity of an ArrayList instance before adding a large number of elements using the ensureCapacity operation. This may reduce the amount of incremental reallocation.
    这里说到ArrayList的一个容量的问题,如果想要Add数量比较多的元素,最好要使用该类
的一个叫做ensureCapacity的方法来确保ArrayList实例具有你所要指定的一个最小的容量。这样的话,对于大容量ArrayList实例,应该就可以由JDK正确地指定到内存中了,这些内存段是否连续,当然是由JDK来处理的问题了。至于这个最小的容量可以达到多少,这里没有讲明,不过应该是和内存多少和每个元素的大小有关系的吧,不好一概而论!
    防止out of memory,有一个方法,就是根据每次调用add方法的时候add的返回值(boolean型)来判断!
至于hashTable,与容量有关的是一下的一段:
    The initial capacity controls a tradeoff between wasted space and the need for rehash operations, which are time-consuming. No rehash operations will ever occur if the initial capacity is greater than the maximum number of entries the Hashtable will contain divided by its load factor. However, setting the initial capacity too high can waste space.
    这里提到初始容量和rehash方法,也就是hashtable在初始容量不够用的情况下可以通过
rehash方法自动增加容量,至于可以增加多少,文档也没有提,显然也是类似于ArrayList的吧。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics