@kiraSally
2018-03-12T10:40:46.000000Z
字数 2800
阅读 2192
JAVA COLLECTIONS 源码 1.7版本
- 读者请先阅读笔者的 集合番@LinkedHashMap一文通(1.7版)
- 笔者个人博客 kiraSally的掘金个人博客 感谢支持
LinkedHashMapLinkedHashMap)
public class LinkedHashSet<E>extends HashSet<E>implements Set<E>, Cloneable, java.io.Serializable
- 重点: LinkedHashSet的实现原理
/*** 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.* LinkedHashSet独享该构造器,同时底层维护一个LinkedHashMap:* -- 借用其的迭代有序和key不重复特性* 因此在LinkedHashSet中只有其构造器的实现* @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<>(initialCapacity, loadFactor);}
/*** Constructs a new, empty linked hash set with the specified initial* capacity and load factor.* 底层会调用父类的构造方法,构造一个有指定初始容量和加载因子的LinkedHashMap实例* @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);}/*** Constructs a new, empty linked hash set with the specified initial* capacity and the default load factor (0.75).* 底层会调用父类的构造方法,构造一个带指定初始容量和默认加载因子0.75的LinkedHashMap实例* @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);}/*** Constructs a new, empty linked hash set with the default initial* capacity (16) and load factor (0.75).* 底层会调用父类的构造方法,构造一个带默认初始容量16和加载因子0.75的LinkedHashMap实例*/public LinkedHashSet() {super(16, .75f, true);}/*** 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).* 底层会调用父类的构造方法,构造一个足以包含指定collection中所有元素的初始容量和加载因子为0.75的LinkedHashMap实例* @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);}
集合番@LinkedHashSet一文通(1.7版) 由 黄志鹏kira 创作,采用 知识共享 署名-非商业性使用 4.0 国际 许可协议 进行许可。
本站文章除注明转载/出处外,均为本站原创或翻译,转载前请务必署名。