说完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);
}
}