正如你所说,两者之间的区别是:
A 是 的有序版本,它在所有元素中维护一个双链列表。使用此类,而不是在关心迭代顺序时使用此类。当您循环访问 a 时,顺序是不可预测的,而 a 允许您按照元素的插入顺序循环访问这些元素。LinkedHashSet
HashSet
HashSet
HashSet
LinkedHashSet
至于你的问题:
但是在LinkedHashSet的源代码中,只有HashSet的调用构造函数。
答案在于构造函数用于构造基类:LinkedHashSet
public LinkedHashSet(int initialCapacity, float loadFactor) {
super(initialCapacity, loadFactor, true); // <-- boolean dummy argument
}
...
public LinkedHashSet(int initialCapacity) {
super(initialCapacity, .75f, true); // <-- boolean dummy argument
}
...
public LinkedHashSet() {
super(16, .75f, true); // <-- boolean dummy argument
}
...
public LinkedHashSet(Collection<? extends E> c) {
super(Math.max(2*c.size(), 11), .75f, true); // <-- boolean dummy argument
addAll(c);
}
并且(一个示例)描述了一个采用布尔参数的构造函数,如下所示:HashSet
/**
* Constructs a new, empty linked hash set. (This package private
* constructor is only used by LinkedHashSet.) The backing
* HashMap instance is a LinkedHashMap with the specified initial
* capacity and the specified load factor.
*
* @param initialCapacity the initial capacity of the hash map
* @param loadFactor the load factor of the hash map
* @param dummy ignored (distinguishes this
* constructor from other int, float constructor.)
* @throws IllegalArgumentException if the initial capacity is less
* than zero, or if the load factor is nonpositive
*/
HashSet(int initialCapacity, float loadFactor, boolean dummy) {
map = new LinkedHashMap<E,Object>(initialCapacity, loadFactor);
}