1. /*
  2. * @(#)LinkedHashSet.java 1.8 03/01/20
  3. *
  4. * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.util;
  8. /**
  9. * <p>Hash table and linked list implementation of the <tt>Set</tt> interface,
  10. * with predictable iteration order. This implementation differs from
  11. * <tt>HashSet</tt> in that it maintains a doubly-linked list running through
  12. * all of its entries. This linked list defines the iteration ordering,
  13. * which is the order in which elements were inserted into the set
  14. * (<i>insertion-order</i>). Note that insertion order is <i>not</i> affected
  15. * if an element is <i>re-inserted</i> into the set. (An element <tt>e</tt>
  16. * is reinserted into a set <tt>s</tt> if <tt>s.add(e)</tt> is invoked when
  17. * <tt>s.contains(e)</tt> would return <tt>true</tt> immediately prior to
  18. * the invocation.)
  19. *
  20. * <p>This implementation spares its clients from the unspecified, generally
  21. * chaotic ordering provided by {@link HashSet}, without incurring the
  22. * increased cost associated with {@link TreeSet}. It can be used to
  23. * produce a copy of a set that has the same order as the original, regardless
  24. * of the original set's implementation:
  25. * <pre>
  26. * void foo(Set m) {
  27. * Set copy = new LinkedHashSet(m);
  28. * ...
  29. * }
  30. * </pre>
  31. * This technique is particularly useful if a module takes a set on input,
  32. * copies it, and later returns results whose order is determined by that of
  33. * the copy. (Clients generally appreciate having things returned in the same
  34. * order they were presented.)
  35. *
  36. * <p>This class provides all of the optional <tt>Set</tt> operations, and
  37. * permits null elements. Like <tt>HashSet</tt>, it provides constant-time
  38. * performance for the basic operations (<tt>add</tt>, <tt>contains</tt> and
  39. * <tt>remove</tt>), assuming the the hash function disperses elements
  40. * properly among the buckets. Performance is likely to be just slightly
  41. * below that of <tt>HashSet</tt>, due to the added expense of maintaining the
  42. * linked list, with one exception: Iteration over a <tt>LinkedHashSet</tt>
  43. * requires time proportional to the <i>size</i> of the set, regardless of
  44. * its capacity. Iteration over a <tt>HashSet</tt> is likely to be more
  45. * expensive, requiring time proportional to its <i>capacity</i>.
  46. *
  47. * <p>A linked hash set has two parameters that affect its performance:
  48. * <i>initial capacity</i> and <i>load factor</i>. They are defined precisely
  49. * as for <tt>HashSet</tt>. Note, however, that the penalty for choosing an
  50. * excessively high value for initial capacity is less severe for this class
  51. * than for <tt>HashSet</tt>, as iteration times for this class are unaffected
  52. * by capacity.
  53. *
  54. * <p><strong>Note that this implementation is not synchronized.</strong> If
  55. * multiple threads access a linked hash set concurrently, and at least one of
  56. * the threads modifies the set, it <em>must</em> be synchronized externally.
  57. * This is typically accomplished by synchronizing on some object that
  58. * naturally encapsulates the set. If no such object exists, the set should
  59. * be "wrapped" using the <tt>Collections.synchronizedSet</tt>method. This is
  60. * best done at creation time, to prevent accidental unsynchronized access:
  61. * <pre>
  62. * Set s = Collections.synchronizedSet(new LinkedHashSet(...));
  63. * </pre>
  64. *
  65. * <p>The iterators returned by the this class's <tt>iterator</tt> method are
  66. * <em>fail-fast</em>: if the set is modified at any time after the iterator
  67. * is created, in any way except through the iterator's own remove method, the
  68. * iterator will throw a <tt>ConcurrentModificationException</tt>. Thus, in
  69. * the face of concurrent modification, the Iterator fails quickly and
  70. * cleanly, rather than risking arbitrary, non-deterministic behavior at an
  71. * undetermined time in the future.
  72. *
  73. * <p>Note that the fail-fast behavior of an iterator cannot be guaranteed
  74. * as it is, generally speaking, impossible to make any hard guarantees in the
  75. * presence of unsynchronized concurrent modification. Fail-fast iterators
  76. * throw <tt>ConcurrentModificationException</tt> on a best-effort basis.
  77. * Therefore, it would be wrong to write a program that depended on this
  78. * exception for its correctness: <i>the fail-fast behavior of iterators
  79. * should be used only to detect bugs.</i>
  80. *
  81. * <p>This class is a member of the
  82. * <a href="{@docRoot}/../guide/collections/index.html">
  83. * Java Collections Framework</a>.
  84. *
  85. * @author Josh Bloch
  86. * @version 1.8 01/20/03
  87. * @see Object#hashCode()
  88. * @see Collection
  89. * @see Set
  90. * @see HashSet
  91. * @see TreeSet
  92. * @see Hashtable
  93. * @since 1.4
  94. */
  95. public class LinkedHashSet extends HashSet
  96. implements Set, Cloneable, java.io.Serializable {
  97. /**
  98. * Constructs a new, empty linked hash set with the specified initial
  99. * capacity and load factor.
  100. *
  101. * @param initialCapacity the initial capacity of the linked hash set
  102. * @param loadFactor the load factor of the linked hash set.
  103. * @throws IllegalArgumentException if the initial capacity is less
  104. * than zero, or if the load factor is nonpositive.
  105. */
  106. public LinkedHashSet(int initialCapacity, float loadFactor) {
  107. super(initialCapacity, loadFactor, true);
  108. }
  109. /**
  110. * Constructs a new, empty linked hash set with the specified initial
  111. * capacity and the default load factor (0.75).
  112. *
  113. * @param initialCapacity the initial capacity of the LinkedHashSet.
  114. * @throws IllegalArgumentException if the initial capacity is less
  115. * than zero.
  116. */
  117. public LinkedHashSet(int initialCapacity) {
  118. super(initialCapacity, .75f, true);
  119. }
  120. /**
  121. * Constructs a new, empty linked hash set with the default initial
  122. * capacity (16) and load factor (0.75).
  123. */
  124. public LinkedHashSet() {
  125. super(16, .75f, true);
  126. }
  127. /**
  128. * Constructs a new linked hash set with the same elements as the
  129. * specified collection. The linked hash set is created with an initial
  130. * capacity sufficient to hold the elements in the specified collection
  131. * and the default load factor (0.75).
  132. *
  133. * @param c the collection whose elements are to be placed into
  134. * this set.
  135. * @throws NullPointerException if the specified collection is null.
  136. */
  137. public LinkedHashSet(Collection c) {
  138. super(Math.max(2*c.size(), 11), .75f, true);
  139. addAll(c);
  140. }
  141. }