Java_LinkedHashSet工作原理

Hash table and linked list implementation of the Set interface, with predictable iteration order. This implementation differs from HashSet in that it maintains a doubly-linked list running through all of its entries

LinkedHashSet是基於HashSet實現的,在HashSet的基礎上,維持了一個雙向鏈表的關係。可以對比HashMapLinkedHashMap的鏈表維持。

LinkedHashSet:

//直接繼承與HashSet,可序列化
public class LinkedHashSet<E>
    extends HashSet<E>
    implements Set<E>, Cloneable, java.io.Serializable {....}
  //相應的構造方法均是調用HashSet方法
 public LinkedHashSet(int initialCapacity, float loadFactor) {
        super(initialCapacity, loadFactor, true);
    }
 public LinkedHashSet(int initialCapacity) {
        super(initialCapacity, .75f, true);
    }
 public LinkedHashSet() {
        super(16, .75f, true);
    }
 public LinkedHashSet(Collection<? extends E> c) {
        super(Math.max(2*c.size(), 11), .75f, true);
        addAll(c);
    }

總結:LinkedHashSet是在HashSet的基礎上用鏈表維護遍歷出的順序,但是並不能說其是有序的,因爲在存儲的時候,LinkedHashSet還是無序的,只是單獨用一個鏈表來保證迭代出的順序。

發佈了76 篇原創文章 · 獲贊 48 · 訪問量 9萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章