您的位置:首页 > 编程语言 > Java开发

从头认识java-15.7 Map(6)-介绍HashMap的工作原理-装载因子与性能

2017-07-18 15:45 471 查看
这一章节我们通过讨论装载因子与性能,再来介绍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的工作原理。

这一章节就到这里,谢谢。

-----------------------------------

文件夹
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: