ArrayList及HashMap的扩容规则讲解

 更新时间:2019年02月12日 16:23:39   作者:wlmmmm  
今天小编就为大家分享一篇关于ArrayList及HashMap的扩容规则讲解,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧

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;

扩容规则为: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;

扩容规则为:大于oldCapacity的最小的2的n次方整数

/**
 * 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);
}

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对脚本之家的支持。如果你想了解更多相关内容请查看下面相关链接

相关文章

  • JVM的基本介绍以及垃圾回收

    JVM的基本介绍以及垃圾回收

    垃圾回收(Garbage Collection,GC),顾名思义就是释放垃圾占用的空间,防止内存泄露,这篇文章主要给大家介绍了关于JVM垃圾回收的相关资料,需要的朋友可以参考下
    2021-09-09
  • java实现单机版五子棋小游戏

    java实现单机版五子棋小游戏

    这篇文章主要为大家详细介绍了java实现单机版五子棋小游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-12-12
  • Java判断多个时间段是否重合的方法小结

    Java判断多个时间段是否重合的方法小结

    这篇文章主要为大家详细介绍了Java中判断多个时间段是否重合的方法,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下
    2025-02-02
  • Sharding-Proxy分库分表和数据加密使用场景分析

    Sharding-Proxy分库分表和数据加密使用场景分析

    这篇文章主要介绍了Sharding-Proxy分库分表和数据加密使用经验分享,通过场景模拟分析结合示例代码给大家介绍的非常详细,需要的朋友可以参考下
    2022-04-04
  • 详解Spring中singleton bean如何同时服务多个请求

    详解Spring中singleton bean如何同时服务多个请求

    这篇文章主要介绍了详解Spring中singleton bean如何同时服务多个请求
    2023-02-02
  • eclipse springboot工程打war包方法及再Tomcat中运行的方法

    eclipse springboot工程打war包方法及再Tomcat中运行的方法

    这篇文章主要介绍了eclipse springboot工程打war包方法及再Tomcat中运行的方法,本文图文并茂给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
    2019-08-08
  • Java中的TreeSet源码解读

    Java中的TreeSet源码解读

    这篇文章主要介绍了Java中的TreeSet源码解读,TreeSet 是一个 有序集合,它扩展了 AbstractSet 类并实现了 NavigableSet 接口,对象根据其自然顺序以升序排序和存储,该 TreeSet 中使用 平衡树,更具体的一个 红黑树,需要的朋友可以参考下
    2023-09-09
  • PowerJob的ServerDiscoveryService工作流程源码解读

    PowerJob的ServerDiscoveryService工作流程源码解读

    这篇文章主要为大家介绍了PowerJob的ServerDiscoveryService工作流程源码解读,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-12-12
  • Java基础之TreeMap详解

    Java基础之TreeMap详解

    这篇文章主要介绍了Java基础之TreeMap详解,文中有非常详细的代码示例,对正在学习java基础的小伙伴们有非常好的帮助,需要的朋友可以参考下
    2021-04-04
  • 详解IntelliJ IDEA 自定义方法注解模板

    详解IntelliJ IDEA 自定义方法注解模板

    本篇文章主要介绍了IntelliJ IDEA 自定义方法注解模板,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-12-12

最新评论