美文网首页
LinkedHashMap

LinkedHashMap

作者: 贼噶人 | 来源:发表于2020-12-15 20:37 被阅读0次

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;
            }
        }

相关文章

网友评论

      本文标题:LinkedHashMap

      本文链接:https://www.haomeiwen.com/subject/qjfggktx.html