[关闭]
@kiraSally 2018-03-12T18:40:46.000000Z 字数 2800 阅读 1876

集合番@LinkedHashSet一文通(1.7版)

JAVA COLLECTIONS 源码 1.7版本


1.LinkedHashSet的定义

2.LinkedHashSet的数据结构

2.1 类定义

  1. public class LinkedHashSet<E>
  2. extends HashSet<E>
  3. implements Set<E>, Cloneable, java.io.Serializable

2.2 HashSet的friendly构造器

  • 重点: LinkedHashSet的实现原理
  1. /**
  2. * Constructs a new, empty linked hash set. (This package private
  3. * constructor is only used by LinkedHashSet.) The backing
  4. * HashMap instance is a LinkedHashMap with the specified initial
  5. * capacity and the specified load factor.
  6. * LinkedHashSet独享该构造器,同时底层维护一个LinkedHashMap:
  7. * -- 借用其的迭代有序和key不重复特性
  8. * 因此在LinkedHashSet中只有其构造器的实现
  9. * @param initialCapacity the initial capacity of the hash map
  10. * @param loadFactor the load factor of the hash map
  11. * @param dummy ignored (distinguishes this
  12. * constructor from other int, float constructor.)
  13. * @throws IllegalArgumentException if the initial capacity is less
  14. * than zero, or if the load factor is nonpositive
  15. */
  16. HashSet(int initialCapacity, float loadFactor, boolean dummy) {
  17. map = new LinkedHashMap<>(initialCapacity, loadFactor);
  18. }

2.3 构造器

  1. /**
  2. * Constructs a new, empty linked hash set with the specified initial
  3. * capacity and load factor.
  4. * 底层会调用父类的构造方法,构造一个有指定初始容量和加载因子的LinkedHashMap实例
  5. * @param initialCapacity the initial capacity of the linked hash set
  6. * @param loadFactor the load factor of the linked hash set
  7. * @throws IllegalArgumentException if the initial capacity is less
  8. * than zero, or if the load factor is nonpositive
  9. */
  10. public LinkedHashSet(int initialCapacity, float loadFactor) {
  11. super(initialCapacity, loadFactor, true);
  12. }
  13. /**
  14. * Constructs a new, empty linked hash set with the specified initial
  15. * capacity and the default load factor (0.75).
  16. * 底层会调用父类的构造方法,构造一个带指定初始容量和默认加载因子0.75的LinkedHashMap实例
  17. * @param initialCapacity the initial capacity of the LinkedHashSet
  18. * @throws IllegalArgumentException if the initial capacity is less
  19. * than zero
  20. */
  21. public LinkedHashSet(int initialCapacity) {
  22. super(initialCapacity, .75f, true);
  23. }
  24. /**
  25. * Constructs a new, empty linked hash set with the default initial
  26. * capacity (16) and load factor (0.75).
  27. * 底层会调用父类的构造方法,构造一个带默认初始容量16和加载因子0.75的LinkedHashMap实例
  28. */
  29. public LinkedHashSet() {
  30. super(16, .75f, true);
  31. }
  32. /**
  33. * Constructs a new linked hash set with the same elements as the
  34. * specified collection. The linked hash set is created with an initial
  35. * capacity sufficient to hold the elements in the specified collection
  36. * and the default load factor (0.75).
  37. * 底层会调用父类的构造方法,构造一个足以包含指定collection中所有元素的初始容量和加载因子为0.75的LinkedHashMap实例
  38. * @param c the collection whose elements are to be placed into
  39. * this set
  40. * @throws NullPointerException if the specified collection is null
  41. */
  42. public LinkedHashSet(Collection<? extends E> c) {
  43. super(Math.max(2*c.size(), 11), .75f, true);
  44. addAll(c);
  45. }

集合番@LinkedHashSet一文通(1.7版)黄志鹏kira 创作,采用 知识共享 署名-非商业性使用 4.0 国际 许可协议 进行许可。

本站文章除注明转载/出处外,均为本站原创或翻译,转载前请务必署名

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注