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

Java源码剖析之HashMap

2017-03-28 09:43 344 查看
键值对(也就是Map)是我们经常使用的一种数据结构,其中HashMap应该是我们使用最多的一种,因为它速度快,没有别的特殊需求一般我们都会使用HashMap。下面我们对它的源码进行剖析。

1.单位存储对象Entry

HashMap中,一个键值对使用一个Entry

static class Entry<K,V> implements Map.Entry<K,V> {
final K key;
V value;
Entry<K,V> next;
int hash;

/**
* Creates new entry.
*/
Entry(int h, K k, V v, Entry<K,V> n) {
value = v;
next = n;
key = k;
hash = h;
}

public final K getKey() {
return key;
}

public final V getValue() {
return value;
}

public final V setValue(V newValue) {
V oldValue = value;
value = newValue;
return oldValue;
}

public final boolean equals(Object o) {
if (!(o instanceof Map.Entry))
return false;
Map.Entry e = (Map.Entry)o;
Object k1 = getKey();
Object k2 = e.getKey();
if (k1 == k2 || (k1 != null && k1.equals(k2))) {
Object v1 = getValue();
Object v2 = e.getValue();
if (v1 == v2 || (v1 != null && v1.equals(v2)))
return true;
}
return false;
}

public final int hashCode() {
return (key==null   ? 0 : key.hashCode()) ^
(value==null ? 0 : value.hashCode());
}

public final String toString() {
return getKey() + "=" + getValue();
}

/**
* This method is invoked whenever the value in an entry is
* overwritten by an invocation of put(k,v) for a key k that's already
* in the HashMap.
*/
void recordAccess(HashMap<K,V> m) {
}

/**
* This method is invoked whenever the entry is
* removed from the table.
*/
void recordRemoval(HashMap<K,V> m) {
}
}


Entry使用了static修饰,保证了是entry对象是HashMap类共享的而不是对象共享。

Entry内部包含对象Entry next,实际上就是一个单项链表。哈希表的键值对都是存储在其中的。

2.几个关键字段

HashMap中有几个比较重要的字段需要介绍:

(1)table:是一个Entry类型的数组

(2)size:是HashMap的大小,表示键值对的数量

(3)threshold:是HashMap的阈值,用来判断是否需要调整HashMap的容量。threshold=“容量”*loadFactor

(4)loadFactor:加载因子,用于(3)

(5)modCount:用来实现fail-fast机制

3.构造函数

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)Math.min(capacity * loadFactor, MAXIMUM_CAPACITY + 1);
table = new Entry[capacity];
useAltHashing = sun.misc.VM.isBooted() &&
(capacity >= Holder.ALTERNATIVE_HASHING_THRESHOLD);
init();
}
public HashMap(int initialCapacity) {
this(initialCapacity, DEFAULT_LOAD_FACTOR);
}
public HashMap() {
this(DEFAULT_INITIAL_CAPACITY, DEFAULT_LOAD_FACTOR);
}


HashMap构造函数包含有两个参数,初始容量initialCapacity和加载因子loadFactor。没有输入时使用默认值。默认容量为16,加载因子为0.75f。另外设置了最大容量2^30。且当容量capacity(开始为1)小于初始容量时,容量翻倍。也就是说,HashMap中的容量是2的幂,并且,初始化时会创建大小为capacity的桶table。

3.clear()方法

public void clear() {
modCount++;
Entry[] tab = table;
for (int i = 0; i < tab.length; i++)
tab[i] = null;
size = 0;
}


将每个桶置null即可。

4.containKey()方法

public boolean containsKey(Object key) {
return getEntry(key) != null;
}
final Entry<K,V> getEntry(Object key) {
int hash = (key == null) ? 0 : hash(key);       //获取hash值,null储存在第一个桶中,不为null则通过hash()计算hash值
for (Entry<K,V> e = table[indexFor(hash, table.length)];//在该hash值对应的桶中查找键值为key的元素
e != null;
e = e.next) {
Object k;
if (e.hash == hash &&
((k = e.key) == key || (key != null && key.equals(k))))
return e;
}
return null;
}


containKey()方法调用了getEntry()方法,该方法对key进行散列找到对应的桶然后顺序搜索元素。其中key为null时使用第1个桶(table[0])。

5.containValue()方法

public boolean containsValue(Object value) {
if (value == null)
return containsNullValue();

Entry[] tab = table;
for (int i = 0; i < tab.length ; i++)
for (Entry e = tab[i] ; e != null ; e = e.next)
if (value.equals(e.value))
return true;
return false;
}
private boolean containsNullValue() {
Entry[] tab = table;
for (int i = 0; i < tab.length ; i++)
for (Entry e = tab[i] ; e != null ; e = e.next)
if (e.value == null)
return true;
return false;
}


containValue()方法是遍历所有桶中的元素进行匹配查找,相比于containKey()方法要慢很多。另外对null值单独进行查找。

6.entrySet(),values(),keyset()

public Set<Map.Entry<K,V>> entrySet() {
return entrySet0();
}

private Set<Map.Entry<K,V>> entrySet0() {
Set<Map.Entry<K,V>> es = entrySet;
return es != null ? es : (entrySet = new EntrySet());
}

private final class EntrySet extends AbstractSet<Map.Entry<K,V>> {
public Iterator<Map.Entry<K,V>> iterator() {
return newEntryIterator();
}
public boolean contains(Object o) {
if (!(o instanceof Map.Entry))
return false;
Map.Entry<K,V> e = (Map.Entry<K,V>) o;
Entry<K,V> candidate = getEntry(e.getKey());
return candidate != null && candidate.equals(e);
}
public boolean remove(Object o) {
return removeMapping(o) != null;
}
public int size() {
return size;
}
public void clear() {
HashMap.this.clear();
}
}
private final class EntryIterator extends HashIterator<Map.Entry<K,V>> {
public Map.Entry<K,V> next() {
return nextEntry();
}
}
private abstract class HashIterator<E> implements Iterator<E> {
Entry<K,V> next;        // next entry to return 下一元素
int expectedModCount;   // For fast-fail
int index;              // current slot     当前索引
Entry<K,V> current;     // current entry    当前元素

HashIterator() {
expectedModCount = modCount;
if (size > 0) { // advance to first entry
Entry[] t = table;
//index初始值为0,并且找到第一个不为null的桶
while (index < t.length && (next = t[index++]) == null)
;
}
}

public final boolean hasNext() {
return next != null;
}

final Entry<K,V> nextEntry() {
if (modCount != expectedModCount)
throw new ConcurrentModificationException();
Entry<K,V> e = next;
if (e == null)
throw new NoSuchElementException();

if ((next = e.next) == null) { //若entry的下一个为null,说明此桶已经遍历完了 需要转到下一个桶
Entry[] t = table;
while (index < t.length && (next = t[index++]) == null)
;
}
current = e;
return e;
}

public void remove() {
if (current == null)
throw new IllegalStateException();
if (modCount != expectedModCount)
throw new ConcurrentModificationException();
Object k = current.key;
current = null;
HashMap.this.removeEntryForKey(k);
expectedModCount = modCount;
}
}


HashMap内包含了一个继承了AbstractSet的类EntrySet,然后通过其迭代器Entryiterator来进行遍历。HashIterator迭代器实现了key迭代器,value迭代器和entry迭代器三个子类。

7.get()方法

public V get(Object key) {
if (key == null)
return getForNullKey();
Entry<K,V> entry = getEntry(key);

return null == entry ? null : entry.getValue();
}
private V getForNullKey() {
for (Entry<K,V> e = table[0]; e != null; e = e.next) {
if (e.key == null)
return e.value;
}
return null;
}


get()方法同containKey()方法,直接通过getEntry()方法取得对应Entry元素即可。

8.put()方法

public V put(K key, V value) {
if (key == null)
return putForNullKey(value);
int hash = hash(key);                           //对key进行散列
int i = indexFor(hash, table.length);                   //得到桶的索引值
for (Entry<K,V> e = table[i]; e != null; e = e.next) {
Object k;
if (e.hash == hash && ((k = e.key) == key || key.equals(k))) {  //若key存在 则更新,并返回原值
V oldValue = e.value;
e.value = value;
e.recordAccess(this);
return oldValue;
}
}

modCount++;
addEntry(hash, key, value, i);                      //key不存在 添加新值
return null;
}
private V putForNullKey(V value) {
for (Entry<K,V> e = table[0]; e != null; e = e.next) {
if (e.key == null) {
V oldValue = e.value;
e.value = value;
e.recordAccess(this);
return oldValue;
}
}
modCount++;
addEntry(0, null, value, 0);
return null;
}


put()函数实现也很容易理解,获得散列值,得到桶的索引后插入即可。插入时使用的addEntry()函数如下:

void addEntry(int hash, K key, V value, int bucketIndex) {
if ((size >= threshold) && (null != table[bucketIndex])) {
resize(2 * table.length);           //若size大于阈值,则调整HashMap大小
hash = (null != key) ? hash(key) : 0;   //需要重新散列
bucketIndex = indexFor(hash, table.length);
}

createEntry(hash, key, value, bucketIndex);
}
void createEntry(int hash, K key, V value, int bucketIndex) {
Entry<K,V> e = table[bucketIndex];
table[bucketIndex] = new Entry<>(hash, key, value, e);
size++;
}


这里是先调整桶的大小,因此调整过后需要重新散列。然后再放入桶中。

9.remove()方法

public V remove(Object key) {
Entry<K,V> e = removeEntryForKey(key);
return (e == null ? null : e.value);
}
final Entry<K,V> removeEntryForKey(Object key) {
int hash = (key == null) ? 0 : hash(key);
int i = indexFor(hash, table.length);
Entry<K,V> prev = table[i];     //Entry单向链表的父节点
Entry<K,V> e = prev;            //当前Entry节点

while (e != null) {
Entry<K,V> next = e.next;
Object k;
if (e.hash == hash &&
((k = e.key) == key || (key != null && key.equals(k)))) {
modCount++;
size--;
if (prev == e)
table[i] = next;
else
prev.next = next;
e.recordRemoval(this);
return e;
}
prev = e;
e = next;
}

return e;
}


找到桶,找到Entry,删除,就是这么简单!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java hashmap