LinkedHashMap 是如何保证foreach时的put顺序的,其实是通过链表的方式来记录put的顺序
private void linkNodeLast(LinkedHashMap.Entry<K, V> p) {
LinkedHashMap.Entry<K, V> last = this.tail;
this.tail = p;
if (last == null) {
this.head = p;
} else {
p.before = last;
last.after = p;
}
}
final LinkedHashMap.Entry<K, V> nextNode() {
LinkedHashMap.Entry<K, V> e = this.next;
if (LinkedHashMap.this.modCount != this.expectedModCount) {
throw new ConcurrentModificationException();
} else if (e == null) {
throw new NoSuchElementException();
} else {
this.current = e;
this.next = e.after;
return e;
}
}
网友评论