LinkedHashSet.java 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /* LinkedHashSet.java -- a set backed by a LinkedHashMap, for linked
  2. list traversal.
  3. Copyright (C) 2001, 2004, 2005, 2007 Free Software Foundation, Inc.
  4. This file is part of GNU Classpath.
  5. GNU Classpath is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2, or (at your option)
  8. any later version.
  9. GNU Classpath is distributed in the hope that it will be useful, but
  10. WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with GNU Classpath; see the file COPYING. If not, write to the
  15. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  16. 02110-1301 USA.
  17. Linking this library statically or dynamically with other modules is
  18. making a combined work based on this library. Thus, the terms and
  19. conditions of the GNU General Public License cover the whole
  20. combination.
  21. As a special exception, the copyright holders of this library give you
  22. permission to link this library with independent modules to produce an
  23. executable, regardless of the license terms of these independent
  24. modules, and to copy and distribute the resulting executable under
  25. terms of your choice, provided that you also meet, for each linked
  26. independent module, the terms and conditions of the license of that
  27. module. An independent module is a module which is not derived from
  28. or based on this library. If you modify this library, you may extend
  29. this exception to your version of the library, but you are not
  30. obligated to do so. If you do not wish to do so, delete this
  31. exception statement from your version. */
  32. package java.util;
  33. import java.io.Serializable;
  34. /**
  35. * This class provides a hashtable-backed implementation of the
  36. * Set interface, with predictable traversal order.
  37. * <p>
  38. *
  39. * It uses a hash-bucket approach; that is, hash collisions are handled
  40. * by linking the new node off of the pre-existing node (or list of
  41. * nodes). In this manner, techniques such as linear probing (which
  42. * can cause primary clustering) and rehashing (which does not fit very
  43. * well with Java's method of precomputing hash codes) are avoided. In
  44. * addition, this maintains a doubly-linked list which tracks insertion
  45. * order. Note that the insertion order is not modified if an
  46. * <code>add</code> simply reinserts an element in the set.
  47. * <p>
  48. *
  49. * One of the nice features of tracking insertion order is that you can
  50. * copy a set, and regardless of the implementation of the original,
  51. * produce the same results when iterating over the copy. This is possible
  52. * without needing the overhead of <code>TreeSet</code>.
  53. * <p>
  54. *
  55. * Under ideal circumstances (no collisions), LinkedHashSet offers O(1)
  56. * performance on most operations. In the worst case (all elements map
  57. * to the same hash code -- very unlikely), most operations are O(n).
  58. * <p>
  59. *
  60. * LinkedHashSet accepts the null entry. It is not synchronized, so if
  61. * you need multi-threaded access, consider using:<br>
  62. * <code>Set s = Collections.synchronizedSet(new LinkedHashSet(...));</code>
  63. * <p>
  64. *
  65. * The iterators are <i>fail-fast</i>, meaning that any structural
  66. * modification, except for <code>remove()</code> called on the iterator
  67. * itself, cause the iterator to throw a
  68. * {@link ConcurrentModificationException} rather than exhibit
  69. * non-deterministic behavior.
  70. *
  71. * @author Eric Blake (ebb9@email.byu.edu)
  72. * @see Object#hashCode()
  73. * @see Collection
  74. * @see Set
  75. * @see HashSet
  76. * @see TreeSet
  77. * @see Collections#synchronizedSet(Set)
  78. * @since 1.4
  79. * @status updated to 1.4
  80. */
  81. public class LinkedHashSet<T> extends HashSet<T>
  82. implements Set<T>, Cloneable, Serializable
  83. {
  84. /**
  85. * Compatible with JDK 1.4.
  86. */
  87. private static final long serialVersionUID = -2851667679971038690L;
  88. /**
  89. * Construct a new, empty HashSet whose backing HashMap has the default
  90. * capacity (11) and loadFactor (0.75).
  91. */
  92. public LinkedHashSet()
  93. {
  94. super();
  95. }
  96. /**
  97. * Construct a new, empty HashSet whose backing HashMap has the supplied
  98. * capacity and the default load factor (0.75).
  99. *
  100. * @param initialCapacity the initial capacity of the backing HashMap
  101. * @throws IllegalArgumentException if the capacity is negative
  102. */
  103. public LinkedHashSet(int initialCapacity)
  104. {
  105. super(initialCapacity);
  106. }
  107. /**
  108. * Construct a new, empty HashSet whose backing HashMap has the supplied
  109. * capacity and load factor.
  110. *
  111. * @param initialCapacity the initial capacity of the backing HashMap
  112. * @param loadFactor the load factor of the backing HashMap
  113. * @throws IllegalArgumentException if either argument is negative, or
  114. * if loadFactor is POSITIVE_INFINITY or NaN
  115. */
  116. public LinkedHashSet(int initialCapacity, float loadFactor)
  117. {
  118. super(initialCapacity, loadFactor);
  119. }
  120. /**
  121. * Construct a new HashSet with the same elements as are in the supplied
  122. * collection (eliminating any duplicates, of course). The backing storage
  123. * has twice the size of the collection, or the default size of 11,
  124. * whichever is greater; and the default load factor (0.75).
  125. *
  126. * @param c a collection of initial set elements
  127. * @throws NullPointerException if c is null
  128. */
  129. public LinkedHashSet(Collection<? extends T> c)
  130. {
  131. super(c);
  132. }
  133. /**
  134. * Helper method which initializes the backing Map.
  135. *
  136. * @param capacity the initial capacity
  137. * @param load the initial load factor
  138. * @return the backing HashMap
  139. */
  140. HashMap<T, String> init(int capacity, float load)
  141. {
  142. return new LinkedHashMap<T, String>(capacity, load);
  143. }
  144. }