- 浏览: 624093 次
- 性别:
- 来自: 西安
文章分类
最新评论
-
d1438138:
[img][/img]
google api 的一些神奇使用 -
waykingeye:
[i][b][u]引用[list]
[*][img][url] ...
No result defined for action and result input -
tss0823:
...
No result defined for action and result input -
yahier:
有什么办法能够捕捉,然后给出自定义的提示呢
No result defined for action and result input -
chen_lian:
恩恩 按照上面的代码测试一下觉得很对
java创建目录
说完HashMap,这个Map家族中最耀眼的明星后,再来看看HashMap的几个兄弟,首先要介绍的就是LinkedHashMap,这个直接继承HashMap的小弟
private transient Entry<K,V> header; /** * The iteration ordering method for this linked hash map: <tt>true</tt> * for access-order, <tt>false</tt> for insertion-order. * * @serial */ private final boolean accessOrder;
LinkedHashMap特有的的两个实例变量,一个是header,这个就是LinkedHashMap能选择是以插入顺序还是访问顺序的关键,一个是accessOrder用来记录LinkedHashMap到底是以插入顺序还是访问顺序,默认的是false,即插入顺序,当然也可以利用构造函数来实现访问顺序
private static class Entry<K,V> extends HashMap.Entry<K,V> { // These fields comprise the doubly linked list used for iteration. Entry<K,V> before, after; Entry(int hash, K key, V value, HashMap.Entry<K,V> next) { super(hash, key, value, next); } /** * Removes this entry from the linked list. */ private void remove() { before.after = after; after.before = before; } /** * Inserts this entry before the specified existing entry in the list. */ private void addBefore(Entry<K,V> existingEntry) { after = existingEntry; before = existingEntry.before; before.after = this; after.before = this; } /** * This method is invoked by the superclass whenever the value * of a pre-existing entry is read by Map.get or modified by Map.set. * If the enclosing Map is access-ordered, it moves the entry * to the end of the list; otherwise, it does nothing. */ void recordAccess(HashMap<K,V> m) { LinkedHashMap<K,V> lm = (LinkedHashMap<K,V>)m; if (lm.accessOrder) { lm.modCount++; remove(); addBefore(lm.header); } } void recordRemoval(HashMap<K,V> m) { remove(); } }
以保存插入顺序为例,这个LinkedHashMap之所以能存储插入顺序,关键就在于LinkedHashMap的Entry的实现中包括了Entry<K,V> before, after;这样实际上LinkedHashMap的存储上还是依托于父类的Entry[] table,但是他同时保证了Entry中保存了他插入时的前后元素,这样的实现比起sortedMap完全采用双向列表,在效率要好很多,同时也有了插入顺序的保证,具体说:
void addEntry(int hash, K key, V value, int bucketIndex) { createEntry(hash, key, value, bucketIndex); // Remove eldest entry if instructed, else grow capacity if appropriate Entry<K,V> eldest = header.after; if (removeEldestEntry(eldest)) { removeEntryForKey(eldest.key); } else { if (size >= threshold) resize(2 * table.length); } }
父类中当put一个新的Entry时,最后会调用addEntry,这样子类LinkedHashMap override这个方法,
void createEntry(int hash, K key, V value, int bucketIndex) { HashMap.Entry<K,V> old = table[bucketIndex]; Entry<K,V> e = new Entry<K,V>(hash, key, value, old); table[bucketIndex] = e; e.addBefore(header); size++; } private void addBefore(Entry<K,V> existingEntry) { after = existingEntry; before = existingEntry.before; before.after = this; after.before = this; }
LinkedHashMap 中调用了createEntry,这个方法同样是override了父类的方法,比起父类的createEntry,LinkedHashMap加了主要加了 e.addBefore(header);实际上这就是关键,它把新加的元素加到了Header的头上,addBefore完成了双向列表的指向改变。
Entry<K,V> nextEntry() { if (modCount != expectedModCount) throw new ConcurrentModificationException(); if (nextEntry == header) throw new NoSuchElementException(); Entry<K,V> e = lastReturned = nextEntry; nextEntry = e.after; return e; }
这是LinkedHashMap 中LinkedHashIterator的nextEntry()方法,这是map各个set的iterator方法的next的核心,在这个方法中可以看到,实际上就是利用了header和双向列表的after,从而实现了输出有序的iterator。
另外下面这段代码是实现访问顺序的关键,recordAccess同样是LinkedHashMap override了父类的方法 ,这个recordAccess将在put(已有键值),get,putForNullKey中调用,从而实现了一有访问,被访问元素置于header,
void recordAccess(HashMap<K,V> m) { LinkedHashMap<K,V> lm = (LinkedHashMap<K,V>)m; if (lm.accessOrder) { lm.modCount++; remove(); addBefore(lm.header); } }
发表评论
-
struts2远程执行漏洞学习(四)
2013-05-23 00:12 22270x01 最近又有了一个新的struts2漏洞,http:/ ... -
纯转一篇关于方法句柄的,对理解很多java poc帮助很大
2013-04-19 15:16 4237http://book.2cto.com/20130 ... -
CVE-2013-1493 学习
2013-03-25 16:06 28450x01 这个又是一个java CVE,效果前几个一样, ... -
myeclipse崩溃多处理的一个小窍门
2013-01-15 20:23 31060x01 如果大家用了myeclipse10以上版本,忽然间 ... -
CVE-2013-0422 分析2
2013-01-11 23:47 33010x01 http://wcf1987.iteye.c ... -
CVE-2013-0422 学习
2013-01-11 16:26 40590x01 这个是这两天爆出来的,我构建了一个本地测试代码,主 ... -
CVE 2012 0507 分析
2012-12-17 16:00 35190x01 https://github.com/wche ... -
<找工作 十一>生产者消费者 组赛队列
2012-09-25 17:39 1478用阻塞队列实现 import java.util.co ... -
<找工作 十>生产者 消费者模型
2012-09-25 16:54 1105今天被问了个这个问 ... -
<找工作 九> 字符串全排列问题
2012-09-23 22:01 1346public class StringTest { ... -
<找工作 七>leetcode Add Two Numbers
2012-09-13 22:24 3096Add Two Numbers 链表相加 p ... -
<找工作 六>leetcode Median of Two Sorted Arrays
2012-09-13 21:25 3203http://www.leetcode.com/onlinej ... -
java+jfreechart 做股票日线数据查看系统
2012-07-02 19:56 2315如标题所说,有需要jfreechart做股票日线之类的东西的人 ... -
struts2远程执行漏洞学习(三)
2012-02-24 16:27 5084这个是终结部分了。 除了#_memb ... -
struts2远程执行漏洞学习(二)
2012-02-24 13:38 2854http://commons.apache.org/ogn ... -
struts2远程执行漏洞学习(一)
2012-02-23 16:46 2250首先,这个漏洞已经是比较早的一个了,大概影响范围是 ... -
String和input Stream的转换问题
2011-07-27 17:05 3521问题的背景是需求要生 ... -
关于apache解压zip和sleep程序程序退出问题
2011-03-02 16:26 1450前两天写了 http://wcf1987.iteye.com ... -
zip与unzip
2011-01-24 22:39 7525大家在用java做zip压缩解压缩时,java提供了原生的zi ... -
java调用外部程序控制(三)进阶
2011-01-23 16:25 1535接上次的内容,我们在用java调用外部exe,有时会发生exe ...
相关推荐
java中HashMap,LinkedHashMap,TreeMap,HashTable的区别
HashMap,HashTable,LinkedHashMap,TreeMap的区别
HashMap和LinkedHashMap 描述 该项目提供了可在Node.js和浏览器上运行的HashMap和LinkedHashMap类。 它们都是像一样的简化实现 它使用改进的算法生成哈希。 这样可确保在所有铲斗上尽可能广泛地散布。 根据规范,...
主要介绍了 java HashMap,TreeMap与LinkedHashMap的详解的相关资料,这里提供实例代码,帮助大家学习理解 这部分的内容,需要的朋友可以参考下
java中map简单实例,包含插入,读出,遍历代码。内含map使用实例及HashMap,LinkedHashMap,TreeMap的区别
《java编程思想》,Map结合HashMap获取键相关联的值
比较Java原生的 3种Map的效率。 1. TreeMap 2. HashMap 3. ConcurrentSkipListMap 本测试查找方法使用Map的get方法,循环、离散获取。对于ConcurrentSkipListMap,获得顺序片段,可用subMap()方法,提取50w的子序列...
用数据结构的思想实现java中的类hashmap
java.util.map接口,Java集合框架,hashmap、LinkedHashMap
List、ArrayList、Vector及map、HashTable、HashMap分别的区别
// 常用的map接口的实现类有HashMap,LinkedHashMap和TreeMap // HashMap不保证集合中元素的顺序, // LinkedHashMap按插入顺序排序 // TreeMap按自己的意愿进行排序,默认按key值升序排序。 另包含一篇网文:在java...
jsp上使用object[]看不懂吧?写vo太烦琐了?ok,都交给map吧、本工具类的使命就是让map代替所有的vo,让编程变得更美好。 附有详细的使用例子。 java精英团队十年编程精华。
Map,HashMap,TreeMap的使用 很详细额,值得看看
JNI处理hashmap,string等对象的操作,别处绝对没有的
使用jQuery开发HashMap,包含一些基本的功能。
易语言源码易语言HashMap类源码.rar
用hashmap结构将字典文件中的词条装入内存,并基于该结构进行查询
该文档主要详细总结了Java集合的相关知识,包括Collection和Map接口、Collection接口的子接口List和Set接口以及具体的实现类、存储原理等;Map接口的子接口HashMap、LinkedHashMap、TreeMap、Properties等
Java中List、ArrayList、Vector及map、HashTable、HashMap分别的区别.
HashMap存放.doc