public static void main(String[] args) {
int end = 10000000;
HashMap<Integer, Integer> map = new HashMap<>();
long l = System.currentTimeMillis();
for (int i=0;i<end;i++){
map.put(i,i);
}
long l1 = System.currentTimeMillis();
System.out.println("默认容量消耗时间:"+ (l1-l));
HashMap<Integer, Integer> map1 = new HashMap<>(end/2);
long s = System.currentTimeMillis();
for (int i=0;i<end;i++){
map1.put(i,i);
}
long s1 = System.currentTimeMillis();
System.out.println("指定容量5,000,000消耗时间:"+ (s1-s));
HashMap<Integer, Integer> map2 = new HashMap<>(end);
long m = System.currentTimeMillis();
for (int i=0;i<end;i++){
map2.put(i,i);
}
long m1 = System.currentTimeMillis();
System.out.println("指定容量10000000消耗时间:"+ (m1-m));
}
这是我完全复制你代码跑的测试;
多跑几次都会不一样,差别还挺大;
3种方式单独跑差别更是巨大;
单纯的来看这个时间是没有意义的,因为他们执行时的系统条件都不一样了;
断点它的resize()方法会发现,默认初始化会走很多次这个扩容方法;一半的容量只会调一次扩容;第三种不会调扩容方法
一般HashMap对比效率,不是对比初始化的时间长短,而是添加元素、查询元素的效率。
初始化稍微费点时间,但添加元素时键值冲突少,查询效率高,就是一个好的哈希算法。
所以,你应该比较向HashMap里添加一百万的随机元素时的效率。
我稍微改造了下代码,最终得出的运行结果如下:
默认容量消耗时间:5653
指定容量5,000,000消耗时间:3146
指定容量10000000消耗时间:880
因此我感觉,有可能和垃圾回收有关系,垃圾回收即使没有回收到东西,但是也消耗了一定时间
public static void main(String[] args) {
int end = 10000000;
long l;
{
HashMap<Integer, Integer> map = new HashMap<>();
l = System.currentTimeMillis();
for (int i=0;i<end;i++){
map.put(i,i);
}
long l1 = System.currentTimeMillis();
System.out.println("默认容量消耗时间:"+ (l1-l));
System.gc();
}
{
HashMap<Integer, Integer> map1 = new HashMap<>(end/2);
long s = System.currentTimeMillis();
for (int i=0;i<end;i++){
map1.put(i,i);
}
long s1 = System.currentTimeMillis();
System.out.println("指定容量5,000,000消耗时间:"+ (s1-s));
System.gc();
}
{
HashMap<Integer, Integer> map2 = new HashMap<>(end);
long m = System.currentTimeMillis();
for (int i=0;i<end;i++){
map2.put(i,i);
}
long m1 = System.currentTimeMillis();
System.out.println("指定容量10000000消耗时间:"+ (m1-m));
System.gc();
}
}
扩容的容量是指数级别增长的,默认初始容量16和你设置的初始容量500000看似差别很大,然而实际上只差了15次扩容。这些次数很容易受到执行时环境条件波动的影响,或许你可以多跑几次看一下结果。