从头认识java-15.7 Map(6)-介绍HashMap的工作原理-装载因子与性能
来源:互联网 发布:淘宝好评模板 编辑:程序博客网 时间:2024/06/07 08:31
这一章节我们通过讨论装载因子与性能,再来介绍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的工作原理。
这一章节就到这里,谢谢。
-----------------------------------
目录
0 0
- 从头认识java-15.7 Map(6)-介绍HashMap的工作原理-装载因子与性能
- 从头认识java-15.7 Map(2)-介绍HashMap的工作原理-put方法
- 从头认识java-15.7 Map(3)-介绍HashMap的工作原理-get方法
- 从头认识java-15.7 Map(4)-介绍HashMap的工作原理-hash碰撞(经常作为面试题)
- 从头认识java-15.7 Map(5)-介绍HashMap的工作原理-Key变了,能不能get出原来的value?(偶尔作为面试题)
- 从头认识java-15.7 Map(7)-TreeMap与LinkedHashMap
- 从头认识java-9.4 List的简介与性能
- 从头认识java-15.7 Map(1)-使用数组来简单模拟Map的创建
- Java HashMap的工作原理 及各种Map区别
- Map,equals,hashCode,遍历Map,装载因子,HashMap,LinkedHashMap
- HashMap 的性能因子
- 从头认识java-9.10 Map
- JAVA HashMap 的工作原理(译)
- Java HashMap的工作原理
- Java HashMap的工作原理
- java HashMap的工作原理
- Java HashMap的工作原理
- Java HashMap的工作原理
- iOS动画编程-View动画[ 2 ] Spring动画
- 170,九宫格算法应用
- 经典SQL语句大全
- 夜神模拟器Android studio无法检测到
- 网络协议
- 从头认识java-15.7 Map(6)-介绍HashMap的工作原理-装载因子与性能
- Unity工具栏的使用(一)
- JDBC学习总结
- C/C++ typedef用法
- 从最大似然到EM算法浅解
- 欢迎使用CSDN-markdown编辑器
- php+html配合方式小结
- unable to connect to the virtual device Genymotion 启动问题
- Hello, Blog