---
title: " Java集合框架——HashMap与Hashtable"
author: "ciding"
date: "2011-12-22T11:12:01Z"
tags: []
categories: ["编程语言"]
slug: "java-hashmap-hashtable"
draft: false
lastmod: "2011-12-22T11:16:59Z"
id: 1320547
url: "https://ciding.iteye.com/blog/1320547"
---

**Java集合框架——HashMap与Hashtable**

先来看看两个类的重点方法，再来比较。（这是我看源代码的思路，你也先去看看吧）

集合最主要的操作：写入，读取，移除。

```
    /**
     * Associates the specified value with the specified key in this map.
     * If the map previously contained a mapping for the key, the old
     * value is replaced.
     *  接合指定的value与指定的key在这个map中，假如以前在这个map中包含这个key,以前  
     *  对应的value将会被替换
     * @param key key with which the specified value is to be associated
     * @param value value to be associated with the specified key
     * @return the previous value associated with <tt>key</tt>, or
     *         <tt>null</tt> if there was no mapping for <tt>key</tt>.
     *         (A <tt>null</tt> return can also indicate that the map
     *         previously associated <tt>null</tt> with <tt>key</tt>.)
     */
    public V put(K key, V value) {
        if (key == null)
            return putForNullKey(value);
        int hash = hash(key.hashCode());
        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))) {
                V oldValue = e.value;
                e.value = value;
                e.recordAccess(this);
                return oldValue;
            }
        }

        modCount++;
        addEntry(hash, key, value, i);
        return null;
    }
```

读源代码时，记得把上面的注释读一下，这是最快的理解方法的方式，不要一看源代码，就想着那几个for。

 

里面有几个关键点：

1：int hash = hash(key.hashCode());  
2：int i = indexFor(hash, table.length);  
3：addEntry(hash, key, value, i);

先说第1点：

```
/**
     * Applies a supplemental hash function to a given hashCode, which
     * defends against poor quality hash functions.  This is critical
     * because HashMap uses power-of-two length hash tables, that
     * otherwise encounter collisions for hashCodes that do not differ
     * in lower bits. Note: Null keys always map to hash 0, thus index 0.
     */
    static int hash(int h) {
        // This function ensures that hashCodes that differ only by
        // constant multiples at each bit position have a bounded
        // number of collisions (approximately 8 at default load factor).
        h ^= (h >>> 20) ^ (h >>> 12);
        return h ^ (h >>> 7) ^ (h >>> 4);
    }
```

 

这个是HashMap的哈唏值的算法：（正好之前看过一篇博文，给出分析图，论坛地址是：[http://www.iteye.com/topic/709945](http://www.iteye.com/topic/709945)）

![](http://dl.iteye.com/upload/attachment/609685/b04f960c-fdd9-36d3-9cf3-23cf18e21185.jpg)

画的很好，所以就引用一下啊，至于分析的话，还是看原地址中的吧。（我说过，让你与我一起来这个过程，所以自己去看）。

 

第2点：

    /\**  
     * Returns index for hash code h.  
     \*/  
    static int indexFor(int h, int length) {  
        return h & (length-1);  
    }

 

这么少？那分析什么啊。这里真看不出来有什么好分析的，可是我运气比较好，看过一篇博文。这个地址忘记了，我就把内容写一下吧。

 

这里的关键点在于length的长度。

它是Entry\[] table的长度，这个与HashMap的初始化时指定的。

先看一下HashMap的三个构造函数：

HashMap(int initialCapacity, float loadFactor)

HashMap(int initialCapacity)

HashMap()

 

从最后一个说起：

public HashMap() {  
        this.loadFactor = DEFAULT\_LOAD\_FACTOR;  
        threshold = (int)(DEFAULT\_INITIAL\_CAPACITY * DEFAULT\_LOAD\_FACTOR);  
        table = new Entry\[DEFAULT\_INITIAL\_CAPACITY];  
        init();  
    }

 

其中：DEFAULT\_LOAD\_FACTOR=0.75;DEFAULT\_INITIAL\_CAPACITY=16;

这是默认值，平时用的是不是这个呢？（今天看了之后，以后可以不用这个了，因为你要知道它的用意。）

第二个就不说了，直接第一个构造函数吧。（记得看一下注释）

```
/**
     * Constructs an empty <tt>HashMap</tt> with the specified initial
     * capacity and load factor.
     *
     * @param  initialCapacity the initial capacity
     * @param  loadFactor      the load factor
     * @throws IllegalArgumentException if the initial capacity is negative
     *         or the load factor is nonpositive
     */
    public HashMap(int initialCapacity, float loadFactor) {
        if (initialCapacity < 0)
            throw new IllegalArgumentException("Illegal initial capacity: " +
                                               initialCapacity);

        //如果你给出的容量大小大于HashMap支持的最大值时，取最大值
        if (initialCapacity > MAXIMUM_CAPACITY)
            initialCapacity = MAXIMUM_CAPACITY;
        if (loadFactor <= 0 || Float.isNaN(loadFactor))
            throw new IllegalArgumentException("Illegal load factor: " +
                                               loadFactor);

        //下面的是关键：获取大于或等于的initialCapacity值的最小的2的n次方。
        // 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();
    }
```

 

看到了吧，这里有个2的n次方。回到先前说的indexFor方法

h&(length-1)，这下知道道理了吧，保证在数组之内循环。（只能设计者太邪恶了，因为你只有把代码全部看懂才知道这个用处，好在分享者众多。）

 

再回到上面说的第3重点：addEntry(hash, key, value, i);

```
    /**
     * 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);
    }
```

 这里真没什么好说的，为了分析与Hashtable的不同，看看这个resize(2 * table.length);再到这个方法中的transfer(newTable);（其实这个在Hashtable与这没什么不同，引大家来是想学一下人家的设计思想，都是两个循环，因为HashMap可以有key为null的值，所以设计的时候，就得考虑进去。至于效率方面，两个没什么差别，但是可以看出在扩大容量时，是一个很耗时的工作。认清这点，虽然比较Hashtable没什么用，但是你在理解HashMap有用，与别的集合比较也有用，难道不是吗？）

```
/**
     * Transfers all entries from current table to newTable.
     */
    void transfer(Entry[] newTable) {
        Entry[] src = table;
        int newCapacity = newTable.length;
        for (int j = 0; j < src.length; j++) {
            Entry<K,V> e = src[j];
            if (e != null) {
                src[j] = null;
                do {
                    Entry<K,V> next = e.next;
                    int i = indexFor(e.hash, newCapacity);
                    e.next = newTable[i];
                    newTable[i] = e;
                    e = next;
                } while (e != null);
            }
        }
    }
```

   
到此put方法就结束了。（看客还有什么好的要说的，就回复吧，因为我分享的同时，也希望能再深入点。）

 

 

 

第二大重点，读取

```
    /**
     * Returns the value to which the specified key is mapped,
     * or {@code null} if this map contains no mapping for the key.
     *
     * <p>More formally, if this map contains a mapping from a key
     * {@code k} to a value {@code v} such that {@code (key==null ? k==null :
     * key.equals(k))}, then this method returns {@code v}; otherwise
     * it returns {@code null}.  (There can be at most one such mapping.)
     *
     * <p>A return value of {@code null} does not <i>necessarily</i>
     * indicate that the map contains no mapping for the key; it's also
     * possible that the map explicitly maps the key to {@code null}.
     * The {@link #containsKey containsKey} operation may be used to
     * distinguish these two cases.
     *
     * @see #put(Object, Object)
     */
    public V get(Object key) {
        if (key == null)
            return getForNullKey();
        int hash = hash(key.hashCode());
        for (Entry<K,V> e = table[indexFor(hash, table.length)];
             e != null;
             e = e.next) {
            Object k;
            if (e.hash == hash && ((k = e.key) == key || key.equals(k)))
                return e.value;
        }
        return null;
    }
```

 

代码本身没什么好说的，这里来看看构造函数对它的影响。

如果每个key的hash值都是不一样的，那就很快；反之，如果多个key的hash值一样，而table的深度就更深，获取的速度就有可能遍历到最后。

      **如果开始就知道 HashMap 会保存多个 key-value 对，可以在创建时就使用较大的初始化容量，如果 HashMap 中 Entry 的数量一直不会超过极限容量（capacity * load factor），HashMap 就无需调用 resize() 方法重新分配 table 数组，从而保证较好的性能。当然，开始就将初始容量设置太高可能会浪费空间（系统需要创建一个长度为 capacity 的 Entry 数组），因此创建 HashMap 时初始化容量设置也需要小心对待。** 

 

再看移除：remove(Object key)，与Hashtable没有什么区别的。

都是要遍历一次。并找到对应的key-value，将其移除。所以要它的时间复杂度o(n)。（等之后看了别的集合再说说。）

 

说完了。到Hashtable了。

构造函数差不多，不说。

不同点：

1，默认值不一样。0.75是一样的，initialCapacity为11。而且没有最大值，最小值是1。与2的30次方=1073741824

为什么没有最大值呢？？？（等写完这博文再去看看，这是突然想到的。）

 

2，hash的算法：直接用的int hash = key.hashCode();

    而int index = (hash & 0x7FFFFFFF) % tab.length;与HashMap区别不大，因为HashMap支持key为null，而且固定的把第0位给了它，所以通过h&(length-1)把0给去掉。而这里的这个就是直接循环，对于null的支持，总是要点代价的。

 

3，就是刚说的对null的支持不同，Hashtable是不支持的。如果从源代码中，可以看到

if (value == null) {  
     throw new NullPointerException();  
 }

不支持value为null，而通过

int hash = key.hashCode();可以知道不支持key不能为null。

而HashMap支持有一个key为null,而value为null的不限制。

 

4，就是看看Hashtable在操作方法前都是有个关键字synchronized，不懂的去看我的博客，地址：[http://ciding.iteye.com/blog/1300110](/blog/1300110) （**Java多线程及线程池专题**）

 

5，这个不是重点，也顺带说一下，Hashtable有一个contains(Object value)方法与containsValue(Object value)方法一样。而HashMap只有containsValue(Object value)方法。说这一点的原因是，在有的博文中乱写

写道

HashMap把Hashtable的contains方法去掉了，改成containsvalue和containsKey。因为contains方法容易让人引起误解。

 只是将两个方法合并而已，引起误解也是因为没有看源代码。

 

先这到了。。。