从头认识java-15.7 Map(6)-介绍HashMap的工作原理-装载因子与性能
2016-01-03 23:31
190 查看
这一章节我们通过讨论装载因子与性能,再来介绍HashMap的工作原理。
1.什么是装载因子?他有什么作用?
下面的代码就是装载因子
作用:就是控制什么时候map需要通过resize方法扩容,然后通过rehash方法把原有的元素复制到新的容器里面
2.装载因子与性能
我们看看上面的add方法,他主要用于put或者putall方法,把元素放到链表里面去的,但是他必须要检测map的大小,来确定是否需要resize。
我们再来看下面hashmap的两个构造函数:
观察上面两个函数,大家注意threshold 这个参数,在默认的构造函数里面threshold就是装载因子与初步容量所决定的,但是下面重写的hashmap构造函数,threshold 是由两个参数来决定的,而我们再回头看resize方法,他的执行与否,是由threshold 来决定的,因此得出结论:是否resize,是由装载因子与初步容量来决定的。
我们再通过观察下面resize的源码,得出一个结论,resize越多,性能越低。
resize的源码:
因为resize需要把旧的元素复制到新的容器里面。
3.性能测试:
输出:
47
---------------
15
上面的测试结果已经可以解释上面的结论。
总结:我们这一章节通过讨论装载因子与性能,再来介绍HashMap的工作原理。
这一章节就到这里,谢谢。
-----------------------------------
目录
1.什么是装载因子?他有什么作用?
下面的代码就是装载因子
/** * The load factor used when none specified in constructor. */ static final float DEFAULT_LOAD_FACTOR = 0.75f;
作用:就是控制什么时候map需要通过resize方法扩容,然后通过rehash方法把原有的元素复制到新的容器里面
2.装载因子与性能
/** * 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) { Entry<K,V> e = table[bucketIndex]; table[bucketIndex] = new Entry<K,V>(hash, key, value, e); if (size++ >= threshold) resize(2 * table.length); }
我们看看上面的add方法,他主要用于put或者putall方法,把元素放到链表里面去的,但是他必须要检测map的大小,来确定是否需要resize。
我们再来看下面hashmap的两个构造函数:
public HashMap() { this.loadFactor = DEFAULT_LOAD_FACTOR; threshold = (int)(DEFAULT_INITIAL_CAPACITY * DEFAULT_LOAD_FACTOR); table = new Entry[DEFAULT_INITIAL_CAPACITY]; init(); }
public HashMap(int initialCapacity, float loadFactor) { if (initialCapacity < 0) throw new IllegalArgumentException("Illegal initial capacity: " + initialCapacity); if (initialCapacity > MAXIMUM_CAPACITY) initialCapacity = MAXIMUM_CAPACITY; if (loadFactor <= 0 || Float.isNaN(loadFactor)) throw new IllegalArgumentException("Illegal load factor: " + loadFactor); // Find a power of 2 >= initialCapacity int capacity = 1; while (capacity < initialCapacity) capacity <<= 1; this.loadFactor = loadFactor; threshold = (int)(capacity * loadFactor); table = new Entry[capacity]; init(); }
观察上面两个函数,大家注意threshold 这个参数,在默认的构造函数里面threshold就是装载因子与初步容量所决定的,但是下面重写的hashmap构造函数,threshold 是由两个参数来决定的,而我们再回头看resize方法,他的执行与否,是由threshold 来决定的,因此得出结论:是否resize,是由装载因子与初步容量来决定的。
我们再通过观察下面resize的源码,得出一个结论,resize越多,性能越低。
resize的源码:
void resize(int newCapacity) { Entry[] oldTable = table; int oldCapacity = oldTable.length; if (oldCapacity == MAXIMUM_CAPACITY) { threshold = Integer.MAX_VALUE; return; } Entry[] newTable = new Entry[newCapacity]; transfer(newTable); table = newTable; threshold = (int)(newCapacity * loadFactor); }
因为resize需要把旧的元素复制到新的容器里面。
3.性能测试:
package com.ray.ch15; import java.util.HashMap; public class Test { public static void main(String[] args) { HashMap<Integer, String> map = new HashMap<Integer, String>(); long startTime = System.currentTimeMillis(); for (int i = 0; i < 100000; i++) { map.put(i, "a"); } long endTime = System.currentTimeMillis(); System.out.println(endTime - startTime); System.out.println("---------------"); HashMap<Integer, String> map2 = new HashMap<Integer, String>(100000, 1f); startTime = System.currentTimeMillis(); for (int i = 0; i < 100000; i++) { map2.put(i, "a"); } endTime = System.currentTimeMillis(); System.out.println(endTime - startTime); } }
输出:
47
---------------
15
上面的测试结果已经可以解释上面的结论。
总结:我们这一章节通过讨论装载因子与性能,再来介绍HashMap的工作原理。
这一章节就到这里,谢谢。
-----------------------------------
目录
相关文章推荐
- Spring注入依赖对象
- org.springframework.web.servlet.DispatcherServlet cannot be cast to javax.servle
- 极限编程,最强N皇后JAVA解题代码,4秒出15皇后,33秒出16皇后
- liunx系统安装jdk的方法
- Spring MVC 4.2 增加 CORS 支持
- j2ee、j2se、ejb、javabean、serverlet、jsp之间关系
- Mac环境下用Java(Sikuli+Robot)实现页游自动化
- Spring+myBatis3
- [Java开发之路](12)JDOM和DOM4J解析XML文档
- spring validate group分类校验
- 09Java语法回顾map集合
- Java集合与数组实现升序排序的算法设计
- Spring JPA Hibernate problem: LazyInitializationException: could not initialize proxy no session
- JAVA 经典算法 百钱买百鸡
- Java:单例模式的七种写法
- 设计并实现一个LRU Cache (java)
- java int与integer的区别
- spring+mybatis项目整合
- JAVA字符编码测试
- 一句话概括下spring框架及spring cloud框架主要组件