欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

ArrayList及HashMap的擴容規(guī)則講解

 更新時間:2019年02月12日 16:23:39   作者:wlmmmm  
今天小編就為大家分享一篇關(guān)于ArrayList及HashMap的擴容規(guī)則講解,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧

1、ArrayList

默認大小為10

/**
 * Default initial capacity.
*/
private static final int DEFAULT_CAPACITY = 10;

最大容量為2^30 - 8

 /**
 * The maximum size of array to allocate.
 * Some VMs reserve some header words in an array.
 * Attempts to allocate larger arrays may result in
 * OutOfMemoryError: Requested array size exceeds VM limit
 */
private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
/**
 * A constant holding the maximum value an {@code int} can
 * have, 2<sup>31</sup>-1.
*/
public static final int  MAX_VALUE = 0x7fffffff;

擴容規(guī)則為:oldCapacity*1.5

/**
 * Increases the capacity to ensure that it can hold at least the
 * number of elements specified by the minimum capacity argument.
 * @param minCapacity the desired minimum capacity
 */
private void grow(int minCapacity) {
  // overflow-conscious code
  int oldCapacity = elementData.length;
  int newCapacity = oldCapacity + (oldCapacity >> 1);
  if (newCapacity - minCapacity < 0)
    newCapacity = minCapacity;
  if (newCapacity - MAX_ARRAY_SIZE > 0)
    newCapacity = hugeCapacity(minCapacity);
  // minCapacity is usually close to size, so this is a win:
  elementData = Arrays.copyOf(elementData, newCapacity);
}

2、HashMap

默認大?。?16

/**
 * The default initial capacity - MUST be a power of two.
*/
static final int DEFAULT_INITIAL_CAPACITY = 1 << 4; // aka 16

最大容量為:2^30

/**
 * The maximum capacity, used if a higher value is implicitly specified
 * by either of the constructors with arguments.
 * MUST be a power of two <= 1<<30.
*/
static final int MAXIMUM_CAPACITY = 1 << 30;

擴容規(guī)則為:大于oldCapacity的最小的2的n次方整數(shù)

/**
 * Adds a new entry with the specified key, value and hash code to
 * the specified bucket. It is the responsibility of this
 * method to resize the table if appropriate.
 * Subclass overrides this to alter the behavior of put method.
 */
void addEntry(int hash, K key, V value, int bucketIndex) {
  if ((size >= threshold) && (null != table[bucketIndex])) {
    resize(2 * table.length);
    hash = (null != key) ? hash(key) : 0;
    bucketIndex = indexFor(hash, table.length);
  }
  createEntry(hash, key, value, bucketIndex);
}

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,謝謝大家對腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請查看下面相關(guān)鏈接

相關(guān)文章

  • 微信公眾號開發(fā)之設(shè)置自定義菜單實例代碼【java版】

    微信公眾號開發(fā)之設(shè)置自定義菜單實例代碼【java版】

    這篇文章主要介紹了微信公眾號開發(fā)之設(shè)置自定義菜單實例代碼,本實例是為了實現(xiàn)在管理后臺實現(xiàn)微信菜單的添加刪除管理。需要的朋友可以參考下
    2018-06-06
  • java高并發(fā)之理解進程和線程

    java高并發(fā)之理解進程和線程

    這篇文章主要給大家介紹了關(guān)于java高并發(fā)進程和線程的內(nèi)容,文中通過示例代碼介紹的非常詳細,對大家學(xué)習(xí)或者使用java具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-10-10
  • Java基礎(chǔ)知識精通注釋與數(shù)據(jù)類型及常量與變量

    Java基礎(chǔ)知識精通注釋與數(shù)據(jù)類型及常量與變量

    本文給大家介紹了Java的注釋與數(shù)據(jù)類型和常量變量,這些都是最基礎(chǔ)的知識,文中通過示例代碼介紹的非常詳細。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-04-04
  • Java移位運算符詳解實例(小結(jié))

    Java移位運算符詳解實例(小結(jié))

    這篇文章主要介紹了Java移位運算符詳解實例(小結(jié)),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-12-12
  • java base64編碼、解碼的三種方式總結(jié)

    java base64編碼、解碼的三種方式總結(jié)

    這篇文章主要介紹了java base64編碼、解碼的三種方式,幫助大家更好的理解和學(xué)習(xí)Java,感興趣的朋友可以了解下
    2020-10-10
  • 關(guān)于Spring Boot對jdbc的支持問題

    關(guān)于Spring Boot對jdbc的支持問題

    這篇文章主要介紹了關(guān)于Spring Boot對jdbc的支持問題,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-04-04
  • Spring Boot配置線程池拒絕策略的場景分析(妥善處理好溢出的任務(wù))

    Spring Boot配置線程池拒絕策略的場景分析(妥善處理好溢出的任務(wù))

    本文通過實例代碼給大家介紹下如何為線程池配置拒絕策略、如何自定義拒絕策略。對Spring Boot配置線程池拒絕策略的相關(guān)知識感興趣的朋友一起看看吧
    2021-09-09
  • Spring Boot2.0整合ES5實現(xiàn)文章內(nèi)容搜索實戰(zhàn)

    Spring Boot2.0整合ES5實現(xiàn)文章內(nèi)容搜索實戰(zhàn)

    這篇文章主要介紹了Spring Boot2.0整合ES5實現(xiàn)文章內(nèi)容搜索實戰(zhàn),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-01-01
  • springBoot詳解集成Swagger流程

    springBoot詳解集成Swagger流程

    Swagger是一個規(guī)范和完整的框架,用于生成、描述、調(diào)用和可視化?Restful?風(fēng)格的?Web?服務(wù)??傮w目標是使客戶端和文件系統(tǒng)作為服務(wù)器以同樣的速度來更新。文件的方法、參數(shù)和模型緊密集成到服務(wù)器端的代碼,允許API來始終保持同步
    2022-06-06
  • SpringBoot使用自動配置xxxAutoConfiguration

    SpringBoot使用自動配置xxxAutoConfiguration

    這篇文章介紹了SpringBoot自動配置xxxAutoConfiguration的使用方法,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-12-12

最新評論