java中HashCode的作用和Map的实现结构

核心提示:Map 是一种数据结构,用来实现key和value 的映射。通过Key可以找到Value。实现类包括 HashMap,LinkedHashMap,TreeMap /** * Applies a supplemental hash function to a given hashCode, which * defends against poor quality hash functions. This is cr

 

 

Map 是一种数据结构,用来实现key和value 的映射。通过Key可以找到Value。实现类包括

HashMap,LinkedHashMap,TreeMap

  1. /**  
  2.  * Applies a supplemental hash function to a given hashCode, which  
  3.  * defends against poor quality hash functions.  This is critical  
  4.  * because HashMap uses power-of-two length hash tables, that  
  5.  * otherwise encounter collisions for hashCodes that do not differ  
  6.  * in lower bits. Note: Null keys always map to hash 0, thus index 0.  
  7.  */  
  8. static int hash(int h) {   
  9.     // This function ensures that hashCodes that differ only by   
  10.     // constant multiples at each bit position have a bounded   
  11.     // number of collisions (approximately 8 at default load factor).   
  12.     h ^= (h >>> 20) ^ (h >>> 12);   
  13.     return h ^ (h >>> 7) ^ (h >>> 4);   
  14. }   
  15.   
  16. /**  
  17.  * Returns index for hash code h.  
  18.  */  
  19. static int indexFor(int h, int length) {   
  20.     return h & (length-1);   
  21. }   
  22.   
  23. /**  
  24.  * Associates the specified value with the specified key in this map.  
  25.  * If the map previously contained a mapping for the key, the old  
  26.  * value is replaced.  
  27.  *  
  28.  * @param key key with which the specified value is to be associated  
  29.  * @param value value to be associated with the specified key  
  30.  * @return the previous value associated with <tt>key</tt>, or  
  31.  *         <tt>null</tt> if there was no mapping for <tt>key</tt>.  
  32.  *         (A <tt>null</tt> return can also indicate that the map  
  33.  *         previously associated <tt>null</tt> with <tt>key</tt>.)  
  34.  */  
  35. public V put(K key, V value) {   
  36.     if (key == null)   
  37.         return putForNullKey(value);   
  38.     int hash = hash(key.hashCode());   
  39.     int i = indexFor(hash, table.length);   
  40.     for (Entry<K,V> e = table[i]; e != null; e = e.next) {   
  41.         Object k;   
  42.         if (e.hash == hash && ((k = e.key) == key || key.equals(k))) {   
  43.             V oldValue = e.value;   
  44.             e.value = value;   
  45.             e.recordAccess(this);   
  46.             return oldValue;   
  47.         }   
  48.     }   
  49.   
  50.     modCount++;   
  51.     addEntry(hash, key, value, i);   
  52.     return null;   
  53. }   

HashMap 是以数组的结构,用哈希函数值跟数组的长度做位与运算,获取对应数组的索引。浏览key值时,不保证顺序。
LinkedHashMap 是以双向列表的结构做实现的,浏览key值时候,可以保证顺序。LinkedHashMap继承HashMap ,不同的是数据存储结构。

TreeMap 是以二叉树实现的Map接口。Map中的key值按照从小到大的顺序排列。key要实现comparable。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值