ensureCapacity增加此 ArrayList 实例的容量,以确保它至少能够容纳最小容量参数所指定的元素数。
扩容原则:
若参数值大于底层数组长度的1.5倍,则数组的长度就扩容为这个参数值;若小于底层数组长度的1.5倍,则数组长度就扩容为底层数组长度的1.5倍。
ensureCapacity提高效率
final int N = 10000000;
Object obj = new Object(); //没用调用ensureCapacity()方法初始化ArrayList对象
ArrayList list = new ArrayList();
long startTime = System.currentTimeMillis();
for(int i=0;i<=N;i++){
list.add(obj);
}
long endTime = System.currentTimeMillis();
System.out.println("没有调用ensureCapacity()方法所用时间:" + (endTime - startTime) + "ms"); //调用ensureCapacity()方法初始化ArrayList对象
list = new ArrayList();
startTime = System.currentTimeMillis();
list.ensureCapacity(N);//预先设置list的大小
for(int i=0;i<=N;i++){
list.add(obj);
}
endTime = System.currentTimeMillis();
System.out.println("调用ensureCapacity()方法所用时间:" + (endTime - startTime) + "ms");
没有调用ensureCapacity()方法所用时间:531ms
调用ensureCapacity()方法所用时间:388ms
如果数据量比较小,就没啥差异了
ensureCapacity增加此 ArrayList 实例的容量,以确保它至少能够容纳最小容量参数所指定的元素数。的更多相关文章
- 【转】精简深拷贝ArrayList实例
原文网址:http://gghhgame51333.blog.51cto.com/138362/289383 精简深拷贝ArrayList实例(包括递归和序列化方法) 2007-07-12 16:50 ...
- Java ArrayList调用构造方法传入"容量size"不生效,如何初始化List容量size
创建一个ArrayList对象,传入整型参数 @Test public void arrayListConstructor(){ ArrayList<Object> objects = n ...
- 在原有mysql机器上增加一台实例
采用的是yum install mysql-community-server yum方式安装mysql(社区版) 文章基础上新加一个mysql实例. 这个完全可以直接实战上应用,只要规划好即可 服务器 ...
- Java集合类库list(1)ArrayList实例
public class ArrayListTest { public static void main(String[] args) { //创建空的ArrayList列表 ArrayList al ...
- 如何使用 AWS Auto Scaling 按需动态增加和减少实例
目录 一.背景需求 二.配置步骤 2.1.创建 AMI 2.2.创建负载均衡目标组 2.3.创建 Classic Load Balancer 2.4.创建启动配置 2.5.创建 Auto Scalin ...
- 增加tomcat多实例
第一步:解压 第二步:修改端口 /data/service/tomcat1/conf <Server port="8006" shutdown="SHUTDOWN& ...
- 无法反序列化的java.util.ArrayList实例出来VALUE_STRING的(Can not deserialize instance of java.util.ArrayList out of VALUE_STRING)
解决方法: 设置DeserializationConfig.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY.问题解决.
- 一个问题提交的实例(js原生动画,原生ajax,js引用加参数)
document.writeln("<div id=\"tanchuangwai\" class=\"tanchuangwai\" style= ...
- 内功心法 -- java.util.ArrayList<E> (1)
写在前面的话:读书破万卷,编码如有神--------------------------------------------------------------------下文主要对java.util ...
随机推荐
- Intel汇编指令格式解析
环境: win7_x64旗舰版.VS2015企业版 一.Intel保护模式.实地址模式和虚拟8086模式指令格式(x86) 图在Intel手册2.1章节 1.1)Instruction Prefixe ...
- How To Add Custom Build Steps and Commands To setup.py
转自:https://jichu4n.com/posts/how-to-add-custom-build-steps-and-commands-to-setuppy/ A setup.py scrip ...
- 005-docker启动设置环境变量
https://blog.csdn.net/wsbgmofo/article/details/79173920
- spring 普通类注入为null,通过自定义SpringUtils解决
package com.jathams.spring; import org.springframework.beans.BeansException; import org.springframew ...
- Ansible 之动态Inventory文件(二)
上篇主要讲解了Ansible 的安装和配置,并且根据不同的业务场景将服务器的信息存放在Ansible的Inventory中,其实存放这样的数据每次更新都需要我们自动的添加和删除,这样对于我们维护起来很 ...
- 为毛GPU Cache不能移动顶点?
这篇文章属于典型的剥洋葱文,由表及里,逐步引入新的知识点,挖掘最本质的原因.这篇文的逻辑是先假设再证明,按照这个思路去阅读会比较轻松. Maya里的GPU Cache导入的几何体为什么不能编辑顶点?这 ...
- h5课件是什么?h5(html5)怎样实现交互动画开发?-----浅谈h5交互动画课件的优势
目前很多交互课件,尤其幼儿类的交互课件以动画和交互相结合的类型居多,越来越多的教育机构发现了这种课件对于幼儿的吸引力远大于其他类型的课件,随着flash逐渐被市场淘汰,动画和交互相结合的html5跨平 ...
- Android OkHttp Get请求方式
1.导入okhttp-2.7.5.jar和okio-1.11.0.jar 2.Get请求 public void getDataByGet(){ OkHttpClient client = new O ...
- monitor.sh
#!bin/bash message_counts="" succeed_counts="" all_succeed_counts="" f ...
- Python3.4 枚举类型的使用
From: https://majing.io/posts/10000005131196 枚举类型是在Python3.4新增到Python的标准库. 创建枚举 Python提供了两种方法来创建枚举: ...