JavaLinkedHashMap工作原理及实现-Java开发Java经验技巧_第1页
JavaLinkedHashMap工作原理及实现-Java开发Java经验技巧_第2页
JavaLinkedHashMap工作原理及实现-Java开发Java经验技巧_第3页
JavaLinkedHashMap工作原理及实现-Java开发Java经验技巧_第4页
JavaLinkedHashMap工作原理及实现-Java开发Java经验技巧_第5页
已阅读5页,还剩1页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、java linkcdhashmap i作原理及实现-编程开发技术java linkedhashmap工作原理及实现原文出处:yikun1. 概述在理解了#7介绍的hashmap后,我们来学习linkedhashmap的丁作原理及实现。 首先还是类似的,我们写一个简单的linkedhashmap的程序:linkedhashmap<string, integer> lmap 二 new linkedhashmap<string, tnteger> ();lmap. put ("语文",1);lmap. put (数学",2);lmap. pu

2、t ("英语,3);lmap. put (历史,4);lmap. put (政治,5);lmap. put (地理",6);lmap. put ("生物",7);lmap. put ("化学,8);for (entrystring, integer> entry : lmap. entryseto) system. out. println(entry. getkey() + :+ entry. getvalue();运行结果是:12345678我们可以观察到,和hashmap的运行结果不同,linkedhashmap的迭代输出的结 果保

3、持了插入顺序。是什么样的结构使得linkedhashmap具冇如此特性呢?我们述是一样的看看linkedhashmap的内部结构,对它冇一个感性的认识:政治:5生物:7历史:4数学:2语文:1英语:3地理:605610111213化学:8beforerl if accessorder entryset head> after before/ hash0 key next t> value keysetd loadfactor modcount size> table tail threshold values语文数学=2丿英没错,正如官方文档所说:hash table?and

4、?linked list?implementation of the map interface, with predictable iteration order. this implementation differs from ilashmap in that it maintains a?doubly-linked listrunning through all of its entries. this linked list defines the it era tion ordering, which is normally the order in which keys were

5、 inserted into the map (insertion-order).linkedhashmap是hash表和链表的实现,并且依靠着双向链表保证了迭代顺序 是插入的顺序。2. 三个重点实现的函数在hashmap中提到了下面的定义:/ callbacks to allow linkedhashmap post-actionsvoid afternodeaccess(node<k,v> p) void afternodeinsertion(boolcan cvict) void afternoderemoval(node<k, v> p) linkedhashm

6、ap继承于hashmap,因此也重新实现了这3个函数,顾名思义这三 个函数的作用分别是:节点访问后、节点插入后、节点移除后做一些事情。afternodeaccess 函数void afternodeaccess(node<k, v> c) / move node to last linkedhashmap. entry<k,v> last;/如果定义了 accessorder,那么就保证最近访问节点放到最后 if (accessorder && (last 二 tail) !二 e) linkedhashmap. entry<k,v> p =

7、(linkcdllashmap. entryk,v>)c, b 二 p. before, a 二 p. after; p.after = null;if (b = null)head = a;el seb.after 二 a;if (a != null)a.before = b;elselast 二 b;if (last = null)head = p;else p.before = last;last, after 二 p;tail 二 p;+modcount;就是说在进行put之后就算是对节点的访问了,那么这个时候就会更新链表,把 最近访问的放到最后,保证链表。afternodein

8、sertion 函数void afternodeinsertion (boolean evict) / possibly remove eldestlinkedhashmap. entry<k,v> first;/如果定义了移除规则,则执行相应的溢出if (evict && (first 二 head) !二 nul1 && removeeldestentry(first) k key 二 first key;removenode(hash(key), key, null, false, true);如果用户定义了 removeeldestentry

9、的规则,那么便叮以执行相应的移除操作。afternoderemoval 函数void afternoderemoval (node<k, v> e) / unlink/从链表中移除节点linkedhashmap. entry<k,v> p 二(linkedhashmap. entry<k, v>)e, b = p.before, a = p. after; p.before 二 p.after 二 null;if (b = null)head = a;elseb.after 二 a;if (a = null)tail = b;elsea.before 二 b

10、;这个函数是在移除节点后调用的,就是将节点从双向链表小删除。我们从上而3个函数看出来,基本上都是为了保证双向链表中的节点次序或者双 向链表容量所做的一些额外的事情,目的就是保持双向链表中节点的顺序要从 eldest 到 youngesto3. put和get函数put函数在linkedllashmap中未重新实现,只是实现了 aftcrnodcaccess和 afternodeinsertion两个回调函数。get函数则重新实现并加入了 afternodeaccess来保证访问顺序,下面是get函数的具体实现: public v get (object key) node<k, v>

11、; e;if (c = gctnodc(hash(key), key) = null) return null;if (accessorder) afternodeaccess(e);return e.value;值得注意的是,在accessorder模式下,只要执行get或者put等操作的时候, 就会产生structural modification。官方文档是这么描述的:a structural modification is any operation that adds or del etes one or more mappi ngs or, i n the case of acce

12、ss-ordered linked hash maps, affects iteration order in insertion-ordered linked hash maps, merely changing the value associated with a key that is already contained in the map is not a structural modification. ?in access-ordered linked hash maps, merely querying the map with get is a struetural modification.不要犯了像 concurrentmodificationexception with

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论