`
xiaoZ5919
  • 浏览: 400452 次
  • 性别: Icon_minigender_1
  • 来自: 安平人@北京
博客专栏
Group-logo
Netty学习笔记
浏览量:72762
社区版块
存档分类
最新评论

HashMap无序与LinkedHashMap有序

    博客分类:
  • java
阅读更多

HashMap为什么是无序?

 

HashMap的数据结构是table[entry],entry是一个链表结构,数据的每个元素是一个链表。不同key,但是具有相同hashcode会落在table[hashcode]的链表上

当使用iterator遍历时,使用如下code:

 

final Entry<K,V> nextEntry() {
            if (modCount != expectedModCount)
                throw new ConcurrentModificationException();
            Entry<K,V> e = next;
            if (e == null)
                throw new NoSuchElementException();

            if ((next = e.next) == null) {//如果链表中的最后一个元素则取table中的下一个element中的链表
                Entry[] t = table; //HashMap中的table[entry]
                while (index < t.length && (next = t[index++]) == null)
                    ;
            }
            current = e;
            return e;
        }
 

 

 

如上代码,顺序每次都是固定的,并且按照table+entry链表的顺序,而不是插入顺序。

LinkedHashMap用额外的链表保证插入顺序

 

  void createEntry(int hash, K key, V value, int bucketIndex) {

        HashMap.Entry<K,V> old = table[bucketIndex];
        Entry<K,V> e = new Entry<>(hash, key, value, old);
        table[bucketIndex] = e;
        e.addBefore(header);//插到链表中
        size++;
    }
 

 

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

 

1
2
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics