1. /*
  2. * @(#)HashSet.java 1.28 03/01/23
  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. * This class implements the <tt>Set</tt> interface, backed by a hash table
  10. * (actually a <tt>HashMap</tt> instance). It makes no guarantees as to the
  11. * iteration order of the set; in particular, it does not guarantee that the
  12. * order will remain constant over time. This class permits the <tt>null</tt>
  13. * element.<p>
  14. *
  15. * This class offers constant time performance for the basic operations
  16. * (<tt>add</tt>, <tt>remove</tt>, <tt>contains</tt> and <tt>size</tt>),
  17. * assuming the hash function disperses the elements properly among the
  18. * buckets. Iterating over this set requires time proportional to the sum of
  19. * the <tt>HashSet</tt> instance's size (the number of elements) plus the
  20. * "capacity" of the backing <tt>HashMap</tt> instance (the number of
  21. * buckets). Thus, it's very important not to set the initial capacity too
  22. * high (or the load factor too low) if iteration performance is important.<p>
  23. *
  24. * <b>Note that this implementation is not synchronized.</b> If multiple
  25. * threads access a set concurrently, and at least one of the threads modifies
  26. * the set, it <i>must</i> be synchronized externally. This is typically
  27. * accomplished by synchronizing on some object that naturally encapsulates
  28. * the set. If no such object exists, the set should be "wrapped" using the
  29. * <tt>Collections.synchronizedSet</tt> method. This is best done at creation
  30. * time, to prevent accidental unsynchronized access to the <tt>HashSet</tt>
  31. * instance:
  32. *
  33. * <pre>
  34. * Set s = Collections.synchronizedSet(new HashSet(...));
  35. * </pre><p>
  36. *
  37. * The iterators returned by this class's <tt>iterator</tt> method are
  38. * <i>fail-fast</i>: if the set is modified at any time after the iterator is
  39. * created, in any way except through the iterator's own <tt>remove</tt>
  40. * method, the Iterator throws a <tt>ConcurrentModificationException</tt>.
  41. * Thus, in the face of concurrent modification, the iterator fails quickly
  42. * and cleanly, rather than risking arbitrary, non-deterministic behavior at
  43. * an undetermined time in the future.
  44. *
  45. * <p>Note that the fail-fast behavior of an iterator cannot be guaranteed
  46. * as it is, generally speaking, impossible to make any hard guarantees in the
  47. * presence of unsynchronized concurrent modification. Fail-fast iterators
  48. * throw <tt>ConcurrentModificationException</tt> on a best-effort basis.
  49. * Therefore, it would be wrong to write a program that depended on this
  50. * exception for its correctness: <i>the fail-fast behavior of iterators
  51. * should be used only to detect bugs.</i><p>
  52. *
  53. * This class is a member of the
  54. * <a href="{@docRoot}/../guide/collections/index.html">
  55. * Java Collections Framework</a>.
  56. *
  57. * @author Josh Bloch
  58. * @version 1.28, 01/23/03
  59. * @see Collection
  60. * @see Set
  61. * @see TreeSet
  62. * @see Collections#synchronizedSet(Set)
  63. * @see HashMap
  64. * @since 1.2
  65. */
  66. public class HashSet extends AbstractSet
  67. implements Set, Cloneable, java.io.Serializable
  68. {
  69. static final long serialVersionUID = -5024744406713321676L;
  70. private transient HashMap map;
  71. // Dummy value to associate with an Object in the backing Map
  72. private static final Object PRESENT = new Object();
  73. /**
  74. * Constructs a new, empty set; the backing <tt>HashMap</tt> instance has
  75. * default initial capacity (16) and load factor (0.75).
  76. */
  77. public HashSet() {
  78. map = new HashMap();
  79. }
  80. /**
  81. * Constructs a new set containing the elements in the specified
  82. * collection. The <tt>HashMap</tt> is created with default load factor
  83. * (0.75) and an initial capacity sufficient to contain the elements in
  84. * the specified collection.
  85. *
  86. * @param c the collection whose elements are to be placed into this set.
  87. * @throws NullPointerException if the specified collection is null.
  88. */
  89. public HashSet(Collection c) {
  90. map = new HashMap(Math.max((int) (c.size()/.75f) + 1, 16));
  91. addAll(c);
  92. }
  93. /**
  94. * Constructs a new, empty set; the backing <tt>HashMap</tt> instance has
  95. * the specified initial capacity and the specified load factor.
  96. *
  97. * @param initialCapacity the initial capacity of the hash map.
  98. * @param loadFactor the load factor of the hash map.
  99. * @throws IllegalArgumentException if the initial capacity is less
  100. * than zero, or if the load factor is nonpositive.
  101. */
  102. public HashSet(int initialCapacity, float loadFactor) {
  103. map = new HashMap(initialCapacity, loadFactor);
  104. }
  105. /**
  106. * Constructs a new, empty set; the backing <tt>HashMap</tt> instance has
  107. * the specified initial capacity and default load factor, which is
  108. * <tt>0.75</tt>.
  109. *
  110. * @param initialCapacity the initial capacity of the hash table.
  111. * @throws IllegalArgumentException if the initial capacity is less
  112. * than zero.
  113. */
  114. public HashSet(int initialCapacity) {
  115. map = new HashMap(initialCapacity);
  116. }
  117. /**
  118. * Constructs a new, empty linked hash set. (This package private
  119. * constructor is only used by LinkedHashSet.) The backing
  120. * HashMap instance is a LinkedHashMap with the specified initial
  121. * capacity and the specified load factor.
  122. *
  123. * @param initialCapacity the initial capacity of the hash map.
  124. * @param loadFactor the load factor of the hash map.
  125. * @param dummy ignored (distinguishes this
  126. * constructor from other int, float constructor.)
  127. * @throws IllegalArgumentException if the initial capacity is less
  128. * than zero, or if the load factor is nonpositive.
  129. */
  130. HashSet(int initialCapacity, float loadFactor, boolean dummy) {
  131. map = new LinkedHashMap(initialCapacity, loadFactor);
  132. }
  133. /**
  134. * Returns an iterator over the elements in this set. The elements
  135. * are returned in no particular order.
  136. *
  137. * @return an Iterator over the elements in this set.
  138. * @see ConcurrentModificationException
  139. */
  140. public Iterator iterator() {
  141. return map.keySet().iterator();
  142. }
  143. /**
  144. * Returns the number of elements in this set (its cardinality).
  145. *
  146. * @return the number of elements in this set (its cardinality).
  147. */
  148. public int size() {
  149. return map.size();
  150. }
  151. /**
  152. * Returns <tt>true</tt> if this set contains no elements.
  153. *
  154. * @return <tt>true</tt> if this set contains no elements.
  155. */
  156. public boolean isEmpty() {
  157. return map.isEmpty();
  158. }
  159. /**
  160. * Returns <tt>true</tt> if this set contains the specified element.
  161. *
  162. * @param o element whose presence in this set is to be tested.
  163. * @return <tt>true</tt> if this set contains the specified element.
  164. */
  165. public boolean contains(Object o) {
  166. return map.containsKey(o);
  167. }
  168. /**
  169. * Adds the specified element to this set if it is not already
  170. * present.
  171. *
  172. * @param o element to be added to this set.
  173. * @return <tt>true</tt> if the set did not already contain the specified
  174. * element.
  175. */
  176. public boolean add(Object o) {
  177. return map.put(o, PRESENT)==null;
  178. }
  179. /**
  180. * Removes the specified element from this set if it is present.
  181. *
  182. * @param o object to be removed from this set, if present.
  183. * @return <tt>true</tt> if the set contained the specified element.
  184. */
  185. public boolean remove(Object o) {
  186. return map.remove(o)==PRESENT;
  187. }
  188. /**
  189. * Removes all of the elements from this set.
  190. */
  191. public void clear() {
  192. map.clear();
  193. }
  194. /**
  195. * Returns a shallow copy of this <tt>HashSet</tt> instance: the elements
  196. * themselves are not cloned.
  197. *
  198. * @return a shallow copy of this set.
  199. */
  200. public Object clone() {
  201. try {
  202. HashSet newSet = (HashSet)super.clone();
  203. newSet.map = (HashMap)map.clone();
  204. return newSet;
  205. } catch (CloneNotSupportedException e) {
  206. throw new InternalError();
  207. }
  208. }
  209. /**
  210. * Save the state of this <tt>HashSet</tt> instance to a stream (that is,
  211. * serialize this set).
  212. *
  213. * @serialData The capacity of the backing <tt>HashMap</tt> instance
  214. * (int), and its load factor (float) are emitted, followed by
  215. * the size of the set (the number of elements it contains)
  216. * (int), followed by all of its elements (each an Object) in
  217. * no particular order.
  218. */
  219. private void writeObject(java.io.ObjectOutputStream s)
  220. throws java.io.IOException {
  221. // Write out any hidden serialization magic
  222. s.defaultWriteObject();
  223. // Write out HashMap capacity and load factor
  224. s.writeInt(map.capacity());
  225. s.writeFloat(map.loadFactor());
  226. // Write out size
  227. s.writeInt(map.size());
  228. // Write out all elements in the proper order.
  229. for (Iterator i=map.keySet().iterator(); i.hasNext(); )
  230. s.writeObject(i.next());
  231. }
  232. /**
  233. * Reconstitute the <tt>HashSet</tt> instance from a stream (that is,
  234. * deserialize it).
  235. */
  236. private void readObject(java.io.ObjectInputStream s)
  237. throws java.io.IOException, ClassNotFoundException {
  238. // Read in any hidden serialization magic
  239. s.defaultReadObject();
  240. // Read in HashMap capacity and load factor and create backing HashMap
  241. int capacity = s.readInt();
  242. float loadFactor = s.readFloat();
  243. map = (this instanceof LinkedHashSet ?
  244. new LinkedHashMap(capacity, loadFactor) :
  245. new HashMap(capacity, loadFactor));
  246. // Read in size
  247. int size = s.readInt();
  248. // Read in all elements in the proper order.
  249. for (int i=0; i<size; i++) {
  250. Object e = s.readObject();
  251. map.put(e, PRESENT);
  252. }
  253. }
  254. }