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

346 阅读3分钟
原文链接: www.zybuluo.com
@kiraSally 2017-07-24 10:52 字数 2522 阅读 0

LinkedHashSet一文同

JAVA COLLECTIONS 源码


1.什么是LinkedHashSet

  • 迭代有序,非重复,非线程安全的HashSet,底层数据结构为LinkedHashMap
  • 相比于HashSet,LinkedHashSet多维护着一个运行于所有条目的双重链接列表(doubly-linked list )
  • 此链接列表定义了迭代顺序,该迭代顺序可为插入顺序或是访问顺序(参见LinkedHashMap
  • 适用于存储有序的不重复值的场景,同时也是一个多态的典型实例

2.LinkedHashSet的数据结构

  • 类定义
  1. public class LinkedHashSet<E>
  2. extends HashSet<E>
  3. implements Set<E>, Cloneable, java.io.Serializable
  • 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. }
  • 构造器
  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. }
添加新批注
在作者公开此批注前,只有你和作者可见。
  • 私有
  • 公开
  • 删除
查看更早的 5 条回复
回复批注