HsahMap的工做原理

  只要是学习Java或者是Java程序员,都知道而且用过HaseMap,知道他是用键值对来存储数据而且能够接受null的键和值,今天看到有人提及了HaseMap的工做原理,所以我在这里把他记录下来,以便本身在之后忘了的时候阅读了解。html

  HaseMap的put()与get()方法的工做原理:程序员

  1、Put :

  让咱们看下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>.)

  */

 publicV put(K key, V value) {

  if(key ==null)

   returnputForNullKey(value);

  inthash = hash(key.hashCode());

  inti = 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);

    returnoldValue;

   }

  }

 

  modCount++;

  addEntry(hash, key, value, i);

  returnnull;

 }

如今咱们一步一步来分析上面的代码。 对key作null检查。若是key是null,会被存储到table[0],由于null的hash值老是0。 
key的hashcode()方法会被调用,而后计算hash值。hash值用来找到存储Entry对象的数组的索引。有时候hash函数可能写的很
很差,因此JDK的设计者添加了另外一个叫作hash()的方法,它接收刚才计算的hash值做为参数。若是你想了解更多关于hash()函
数的东西,能够参考:hashmap中的hash和indexFor方法indexFor(hash,table.length)用来计算在table数组中存储Entry对
象的精确的索引。 在咱们的例子中已经看到,若是两个key有相同的hash值(也叫冲突),他们会以链表的形式来存储。因此,这里咱们就迭代链表。 · 若是在刚才计算出来的索引位置没有元素,直接把Entry对象放在那个索引上。 · 若是索引上有元素,而后会进行迭代,一直到Entry->next是null。当前的Entry对象变成链表的下一个节点。 · 若是咱们再次放入一样的key会怎样呢?逻辑上,它应该替换老的value。事实上,它确实是这么作的。在迭代的过程当中,会调用equals()方法来检查key的相等性(key.equals(k)),若是
这个方法返回true,它就会用当前Entry的value来替换以前的value。 Get: 如今咱们来看下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><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 http://www.heyzc.com/pro/45.html

  * explicitly maps the key to {@code null}. The {@link #containsKey

  * containsKey} operation may be used to distinguish these two cases.

  *

  * @see #put(Object, Object)

  */

 publicV get(Object key) {

  if(key ==null)

   returngetForNullKey();

  inthash = 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)))

    returne.value;

  }

  returnnull;

 }
 

 


当你理解了hashmap的put的工做原理,理解get的工做原理就很是简单了。当你传递一个key从hashmap总获取value的时候: 对key进行null检查。若是key是null,table[0]这个位置的元素将被返回。 key的hashcode()方法被调用,而后计算hash值。 indexFor(hash,table.length)用来计算要获取的Entry对象在table数组中的精确的位置,使用刚才计算的hash值。 在获取了table数组的索引以后,会迭代链表,调用equals()方法检查key的相等性,若是equals()方法返回true,get方法返回Entry对象的value,不然,返回null。
要牢记如下关键点:

· HashMap有一个叫作Entry的内部类,它用来存储key-value对。 · 上面的Entry对象是存储在一个叫作table的Entry数组中。 · table的索引在逻辑上叫作“桶”(bucket),它存储了链表的第一个元素。 · key的hashcode()方法用来找到Entry对象所在的桶。 · 若是两个key有相同的hash值,他们会被放在table数组的同一个桶里面。 · key的equals()方法用来确保key的惟一性。 
相关文章
相关标签/搜索