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

总结

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

相关推荐:

GraalVM 中 Java Map 与 Python 的双向互操作实践

本文详解如何在GraalVMPolyglot环境中安全、高效地将JavaMap传递给Python脚本,调用其方法获取值,并将处理结果(如新Map)返回Java,重点解决AttributeError:foreignobjecthasnoattribute'items'等常见互操作异常。 本文详解如何在...

PySpark Java Gateway Exited 错误的根源与解决方案

本文详解pyspark报错`[java_gateway_exited]javagatewayprocessexitedbeforesendingitsportnumber`的根本原因——虚拟环境(venv)隔离导致`java_home`和系统级java路径未被正确继承,并提供可落地的修复步骤与最佳实...

python与java用途区别有哪些

Python适合快速开发、数据科学、AI建模、轻量Web服务及自动化脚本;Java适合大型企业系统、Android开发、高并发中间件及需长期稳定维护的场景。 Python和Java虽然都能做通用开发,但实际用起来分工很明确——不是“哪个更好”,而是“谁更合适”。关键看你要解决什么问题、在什么环境里跑...

Python配置文件加载_ini与json解析【教程】

应根据实际需求选择:ini适合分节、带注释的简单配置(如数据库连接),用configparser读取;json适合嵌套复杂、跨语言共享的配置(如API参数),用json模块加载。 Python读取配置文件,ini和json是最常用的两种格式。选哪种不取决于“哪个更好”,而要看实际需求:ini更适合分...

redis怎么使用java

使用redisjava库需:1.添加依赖(jedis3.6.3);2.连接到redis服务器(指定主机和端口);3.进行基本操作(设置/获取值、检查键是否存在、删除键);4.执行复杂操作(哈希表、列表、集合、有序集合);5.发布/订阅消息(使用jedispubsub);6.关闭连接(jedis.cl...

10 对 -3 取余,结果是 1 还是 -2?Java 和 MySQL 的结果为何不同?

10对-3取余结果:1还是-2? 问题: 对-3取余时,10的余数是1还是-2?使用java和mysql计算得出的结果不同,这是否意味着这些函数存在错误? 答案: 余数正负 余数可以是正数,也可以是负数。数学上计算余数的公式为:余数=被除数-向下取整(被除数/除数)*除数根据该公式,10对-3取余的...