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

总结

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

相关文章

  • Spring中Bean创建完后打印语句的两种方法

    Spring中Bean创建完后打印语句的两种方法

    这篇文章主要介绍了Spring中Bean创建完后打印语句的两种方法,一个是实现InitializingBean接口,另一个使用@Bean注解和initMethod属性,通过代码示例介绍的非常详细,感兴趣的小伙伴可以参考阅读
    2023-07-07
  • SpringBoot整合Mybatis实现商品评分的项目实践

    SpringBoot整合Mybatis实现商品评分的项目实践

    本文介绍了SpringBoot整合Mybatis-plus框架实现对商品评分的功能实现流程和前端接口实现过程,文中通过示例代码介绍的非常详细,需要的朋友们下面随着小编来一起学习学习吧
    2024-02-02
  • java懒惰评估实现方法

    java懒惰评估实现方法

    这篇文章主要介绍了java懒惰评估如何实现的相关内容及实例,有兴趣的朋友们可以学习参考下。
    2021-05-05
  • Shiro + JWT + SpringBoot应用示例代码详解

    Shiro + JWT + SpringBoot应用示例代码详解

    这篇文章主要介绍了Shiro (Shiro + JWT + SpringBoot应用),本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-06-06
  • 基于Java解决华为机试实现密码截取 

    基于Java解决华为机试实现密码截取 

    这篇文章主要介绍了基于Java解决华为机试实现密码截取,文章围绕主题相关资料展开详细内容,具有一的参考价值,需要的小伙伴可以参考一下,希望对你有所帮助
    2022-02-02
  • SpringBoot深入探究@Conditional条件装配的使用

    SpringBoot深入探究@Conditional条件装配的使用

    这篇文章主要为大家介绍了SpringBoot底层注解@Conditional的使用分析,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-06-06
  • 详解Spring/Spring boot异步任务编程WebAsyncTask

    详解Spring/Spring boot异步任务编程WebAsyncTask

    这篇文章主要介绍了详解Spring/Spring boot异步任务编程WebAsyncTask,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-06-06
  • 探索Java中的IP属地获取技术

    探索Java中的IP属地获取技术

    这篇文章主要为大家介绍了Java中的IP属地获取的技术探索,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-10-10
  • Springboot配置图片虚拟映射示例代码

    Springboot配置图片虚拟映射示例代码

    这篇文章主要给大家介绍了关于Springboot配置图片虚拟映射的相关资料,文中通过实例代码介绍的非常详细,对大家学习或者使用springboot具有一定的参考学习价值,需要的朋友可以参考下
    2021-11-11
  • Spring Boot中如何使用Swagger详解

    Spring Boot中如何使用Swagger详解

    Swagger是一个规范和完整的框架,用于生成、描述、调用和可视化 RESTful风格的Web服务,这篇文章主要给大家介绍了关于Spring Boot中如何使用Swagger的相关资料,需要的朋友可以参考下
    2021-08-08

最新评论