關於LinkedHashSet類:
LinkedHashSet 根據元素的 hashCode 值來決定元素的存儲位置,但它同時使用鏈表維護元素的次序,這使得元素看起來是以插入順序保存的。
LinkedHashSet插入性能略低於 HashSet,但在迭代訪問 Set 裏的全部元素時有很好的性能。
LinkedHashSet:(鏈表,有序,不重複)
LinkedHashSet 是具有可預知迭代順序的Set接口的哈希表和鏈接列表實現。此實現與HashSet的不同之處在於,後者維護着一個運行於所有條目的雙重鏈接列表。此鏈接列表定義了迭代順序,該迭代順序可爲插入順序或是訪問順序。
注意,此實現不是同步的。如果多個線程同時訪問鏈接的哈希Set,而其中至少一個線程修改了該Set,則它必須保持外部同步。
對於LinkedHashSet 而言,它繼承與HashSet、又基於LinkedHashMap來實現的。
LinkedHashSet 底層使用LinkedHashMap來保存所有元素,它繼承與 HashSet,其所有的方法操作上又與HashSet相同,因此LinkedHashSet 的實現上非常簡單,只提供了四個構造方法,並通過傳遞一個標識參數,調用父類的構造器,底層構造一個 LinkedHashMap來實現,在相關操作上與父類 HashSet 的操作相同,直接調用父類 HashSet 的方法即可。
LinkedHashSet源碼分析(JDK8):
LinkedHashSet:基於HashSet,底層實現是一個LinkedHashMap,通過equals()方法比較兩個對象是否相等,初始容量16,加載因子0.75。
使用鏈表維護數據的順序,所以說是有序的,不重複的,對象都是存在底層map的key上,所有允許有且只有一個對象爲null。
LinkedHashSet通過繼承 HashSet,底層使用LinkedHashMap,以很簡單明瞭的方式來實現了其自身的所有功能。
public class LinkedHashSet<E> extends HashSet<E>
implements Set<E>, Cloneable, java.io.Serializable {
private static final long serialVersionUID = -2851667679971038690L;
/**
* 構造一個帶有指定初始容量和加載因子的新空鏈接哈希 set 。底層會調用父類的構造方法,構造一個有指定初始容量和加載因子的 LinkedHashMap 實例。Constructs a new, empty linked hash set with the specified initial
* capacity and load factor.
*
* @param initialCapacity the initial capacity of the linked hash set
* @param loadFactor the load factor of the linked hash set
* @throws IllegalArgumentException if the initial capacity is less
* than zero, or if the load factor is nonpositive
初始容量和加載因子
*/
public LinkedHashSet(int initialCapacity, float loadFactor) {
super(initialCapacity, loadFactor, true);
}
/**
* 構造一個帶指定初始容量和默認加載因子 0.75的新空鏈接哈希 set。
底層會調用父類的構造方法,構造一個帶指定初始容量和默認加載因子 0.75的 LinkedHashMap 實例
Constructs a new, empty linked hash set with the specified initial
* capacity and the default load factor (0.75).
*
* @param initialCapacity the initial capacity of the LinkedHashSet
* @throws IllegalArgumentException if the initial capacity is less
* than zero
*/
public LinkedHashSet(int initialCapacity) {
super(initialCapacity, .75f, true);
}
/**構造一個帶默認初始容量 16和加載因子 0.75 的新空鏈接哈希 set。底層會調用父類的構造方法,構造一個帶默認初始容量 16 和加載因子 0.75的 LinkedHashMap 實例。
* Constructs a new, empty linked hash set with the default initial
* capacity (16) and load factor (0.75).
*/
public LinkedHashSet() {
super(16, .75f, true);
}
/**
* 構造一個與指定 collection 中的元素相同的新鏈接哈希 set。
底層會調用父類的構造方法,構造一個足以包含指定 collection中所有元素的初始容量和加載因子爲 0.75的 LinkedHashMap 實例。
Constructs a new linked hash set with the same elements as the
* specified collection. The linked hash set is created with an initial
* capacity sufficient to hold the elements in the specified collection
* and the default load factor (0.75).
*
* @param c the collection whose elements are to be placed into
* this set
* @throws NullPointerException if the specified collection is null
*/
public LinkedHashSet(Collection<? extends E> c) {
super(Math.max(2*c.size(), 11), .75f, true);
addAll(c);
}
/**
* Creates a <em><a href="Spliterator.html#binding">late-binding</a></em>
* and <em>fail-fast</em> {@code Spliterator} over the elements in this set.
*
* <p>The {@code Spliterator} reports {@link Spliterator#SIZED},
* {@link Spliterator#DISTINCT}, and {@code ORDERED}. Implementations
* should document the reporting of additional characteristic values.
*
* @implNote
* The implementation creates a
* <em><a href="Spliterator.html#binding">late-binding</a></em> spliterator
* from the set's {@code Iterator}. The spliterator inherits the
* <em>fail-fast</em> properties of the set's iterator.
* The created {@code Spliterator} additionally reports
* {@link Spliterator#SUBSIZED}.
*
* @return a {@code Spliterator} over the elements in this set
* @since 1.8
*/
@Override
public Spliterator<E> spliterator() {
return Spliterators.spliterator(this, Spliterator.DISTINCT | Spliterator.ORDERED);
}
}
實例:
LinkedHashSet<String> hs = new LinkedHashSet<>();// 泛型 // 創建並添加元素 hs.add("hello"); hs.add("world"); hs.add("java"); hs.add("world"); hs.add("java"); // 遍歷 for (String s : hs) { System.out.println(s); } |