最通俗易懂的方式讲解HashMap

       HashMap,困扰着很多Java初学者,恰恰又在面试时倍受面试官的青睐,本文结合实例和API文档剖析HashMap的工作原理,希望对面试总结或是初学者有一定的帮助。下面进入正题。

       HashMap,基于哈希表的 Map 接口的实现。此实现提供所有可选的映射操作,并允许使用 null 值和 null 键。

       HashMap的实例有两个参数影响其性能:初始容量(默认16) 和 加载因子(默认0.75)。容量 是哈希表中桶的数量,初始容量只是哈希表在创建时的容量。加载因子 是哈希表在其容量自动增加之前可以达到多满的一种尺度。当哈希表中的条目数超出了加载因子与当前容量的乘积时,则要对该哈希表进行 rehash 操作(即重建内部数据结构),从而哈希表将具有大约两倍的桶数。通常,默认加载因子 (.75) 在时间和空间成本上寻求一种折衷。加载因子过高虽然减少了空间开销,但同时也增加了查询成本。(来自JDK API 1.6.0)

 

       我们来看个非常简单的例子。有一个”国家”(Country)类,我们将要用Country对象作为key,它的首都的名字(String类型)作为value。下面的例子有助于我们理解key-value对在HashMap中是如何存储的。

 

public class Country {
	
	String name;
	long population;
	
	public Country(String name,long population){
		super();
		this.name = name;
		this.population = population;
	}	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public long getPopulation() {
		return population;
	}
	public void setPopulation(long population) {
		this.population = population;
	}

	@Override
	public int hashCode() {
		// TODO Auto-generated method stub
		if(this.name.length()%2==0)
			return 31;
		else {
			return 95;
		}
	}
	@Override
	public boolean equals(Object obj) {
		// TODO Auto-generated method stub
		Country other = (Country)obj;
		if(name.equalsIgnoreCase(other.name))
			return true;
		return false;
	}
}

 关于HashCode()方法请参考链接:http://www.cnblogs.com/batys/archive/2011/10/25/2223942.html

 

 

程序入口:

public class HashMapStructure {

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		Country india=new Country("India",1000);
        Country japan=new Country("Japan",10000);
           
        Country france=new Country("France",2000);
        Country russia=new Country("Russia",20000);
        
        HashMap<Country,String> countryCapitalMap=new HashMap<Country,String>();
        countryCapitalMap.put(india, "Delhi");
        countryCapitalMap.put(japan, "Tokyo");
        countryCapitalMap.put(france, "Paris");
        countryCapitalMap.put(russia, "Moscow");
        
        Iterator<Country> countryCapitalIter = countryCapitalMap.keySet().iterator();
        while(countryCapitalIter.hasNext()){
        	Country countryObj = countryCapitalIter.next();
        	String capital = countryCapitalMap.get(countryObj);
        	System.out.println(countryObj.getName()+"----"+capital);
        }
	}

}

 

     现在,在第20行设置一个断点(定位到该行,执行  Ctrl+Shift+B 添加/去除断点),调试运行java application。程序会停在20行,然后在调试窗口查看countryCapitalMap的值,将会看到如下的结构:

 



 

 

从上图可以观察到以下几点:

    1. 有一个叫做table大小是16的Entry数组,16即为HashMap初始容量。

    2. 这个table数组存储了Entry类的对象。HashMap类有一个叫做Entry的内部类。这个Entry类包含了key-value作为实例变量。我们来看下Entry类的结构。Entry类的结构:

 

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

   3. 每当往hashmap里面存放key-value对的时候,都会为它们实例化一个Entry对象,这个Entry对象就会存储在前面提到的Entry数组table中。示例中创建的Entry对象将会存放的位置(即在table中的精确位置),是根据key的hashcode()方法计算出来的hash值来决定。hash值用来计算key在Entry数组的索引。

    4. 观察上图中数组的索引10,它有一个叫做HashMap$Entry的Entry对象。

    5. 示例往hashmap放了4个key-value对,但是看上去好像只有2个元素!!!这是因为,如果两个元素有相同的hashcode,它们会被放在同一个索引上。问题出现了,该怎么放呢?原来它是以链表(LinkedList)的形式来存储的(逻辑上)。

 

上面的country对象的key-value的hash值是如何计算出来的?返回查看我们Overrite的HashCode()方法可知

 

Japan的Hash值是95,它的长度是奇数。

India的Hash值是95,它的长度是奇数。

Russia的Hash值是31,它的长度是偶数。

France,它的长度是偶数。

          

         接下来,我们根据源码来分析put和get方法的实现。

 

put:

 

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

 

下面是put()方法的代码分析:

 

1. 对key做null检查。如果key是null,会被存储到table[0],因为null的hash值总是0。

 

2. key的hashcode()方法会被调用,然后计算hash值。hash值用来找到存储Entry对象的数组的索引。有时候hash函数可能写的很不好,所以JDK的设计者添加了另一个叫做hash()的方法,它接收刚才计算的hash值作为参数。如果你想了解更多关于hash()函数的东西,可以参考:hashmap中的hash和indexFor方法

 

3. indexFor(hash,table.length)用来计算在table数组中存储Entry对象的精确的索引。

 

4. 在我们的例子中已经看到,如果两个key有相同的hash值(也叫冲突),他们会以链表的形式来存储。所以,这里我们就迭代链表。

 

*如果在刚才计算出来的索引位置没有元素,直接把Entry对象放在那个索引上。

*如果索引上有元素,然后会进行迭代,一直到Entry->next是null。当前的Entry对象变成链表的下一个节点。

*如果我们再次放入同样的key会怎样呢?逻辑上,它应该替换老的value。事实上,它确实是这么做的。在迭代的过程中,会调用equals()方法来检查key的相等性(key.equals(k)),如果这个方法返回true,它就会用当前Entry的value来替换之前的value。

 

get:

 

/**
     * 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();
        Entry<K,V> entry = getEntry(key);

        return null == entry ? null : entry.getValue();
    }

 

       当你理解了hashmap的put的工作原理,理解get的工作原理就非常简单了。当你传递一个key从hashmap总获取value的时候:

       1. 对key进行null检查。如果key是null,table[0]这个位置的元素将被返回。

       2. key的hashcode()方法被调用,然后计算hash值。

       3. indexFor(hash,table.length)用来计算要获取的Entry对象在table数组中的精确的位置,使用刚才计算的hash值。

       4. 在获取了table数组的索引之后,会迭代链表,调用equals()方法检查key的相等性,如果equals()方法返回true,get方法返回Entry对象的value,否则,返回null。

 

    到此,相信你对HashMap不再陌生了,学习结束后应当牢记以下关键点:

 

    - HashMap有一个叫做Entry的内部类,它用来存储key-value对。
    - 上面的Entry对象是存储在一个叫做table的Entry数组中。
    - table的索引在逻辑上叫做“桶”(bucket),它存储了链表的第一个元素。
    - key的hashcode()方法用来找到Entry对象所在的桶。
    - 如果两个key有相同的hash值,他们会被放在table数组的同一个桶里面。
    - key的equals()方法用来确保key的唯一性。
    - value对象的equals()和hashcode()方法根本一点用也没有。

 

猜你喜欢

转载自hnulanwei.iteye.com/blog/2219088