JVM里面hashtable和hashmap实现原理及其数.doc

  1. 1、本文档共10页,可阅读全部内容。
  2. 2、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
  3. 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载
  4. 4、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
JVM里面hashtable和hashmap实现原理及其数 JVM里面hashtable和hashmap实现原理及其数据结构 2011年10月07日   在hashtable和hashmap是java里面常见的容器类,   是Java.uitl包下面的类,   那么Hashtable和Hashmap是怎么实现hash键值对配对的呢,我们看看jdk里面的源码,分析下Hashtable的构造方法,put(K, V)加入方法和get(Object)方法就大概明白了。   一、Hashtable的构造方法:Hashtable(int initialCapacity, float loadFactor)   public Hashtable(int initialCapacity, float loadFactor) {   if (initialCapacity 0)   throw new IllegalArgumentException(Illegal Capacity: +   initialCapacity);   if (loadFactor = 0 || Float.isNaN(loadFactor))   throw new IllegalArgumentException(Illegal Load: +loadFactor);   if (initialCapacity==0)   initialCapacity = 1;   this.loadFactor = loadFactor;   table = new Entry[initialCapacity];   threshold = (int)(initialCapacity * loadFactor);   }   这个里面米内有什么好说的,要注意的是table一个是实体数组,输入的initialCapacity表示这个数组的大小,而threshold是一个int值,其中loadFactor默认是0.75,就是说明,当table里面的值到75%的阀值后,快要填满数组了,就会自动双倍扩大数字容量。   而EntryK,V 来自与   private static class EntryK,V implements Map.EntryK,V {   int hash;   K key;   V value;   EntryK,V next;   是一个单项链表,      上图就是Hashtable的表,   二、put(K, V)加入方法   public synchronized V put(K key, V value) {   // Make sure the value is not null   if (value == null) {   throw new NullPointerException();   }   // Makes sure the key is not already in the hashtable.   Entry tab[] = table;   int hash = key.hashCode();   int index = (hash 0x7FFFFFFF) % tab.length;   for (EntryK,V e = tab[index] ; e != null ; e = e.next) {   if ((e.hash == hash) e.key.equals(key)) {   V ld = e.value;   e.value = value;   return old;   }   }   modCount++;   if (count = threshold) {   // Rehash the table if the threshold is exceeded   rehash();   tab = table;   index = (hash 0x7FFFFFFF) % tab.length;   }   // Creates the new entry.   EntryK,V e = tab[index];   tab[index] = new EntryK,V(hash, key, value, e);   count++;   return null;   }   这个看看起来很长,只要注意三点就可以了,   1.index,index是键值的hashcode和0x7FFFFFFF的运算,然后根据数组长度求余值。这样可以定出所在队列名称,   2、   for (E

文档评论(0)

153****9595 + 关注
实名认证
内容提供者

该用户很懒,什么也没介绍

1亿VIP精品文档

相关文档