< prev index next >

src/java.base/share/classes/java/util/HashSet.java

Print this page




  74  *
  75  * <p>This class is a member of the
  76  * <a href="{@docRoot}/java.base/java/util/package-summary.html#CollectionsFramework">
  77  * Java Collections Framework</a>.
  78  *
  79  * @param <E> the type of elements maintained by this set
  80  *
  81  * @author  Josh Bloch
  82  * @author  Neal Gafter
  83  * @see     Collection
  84  * @see     Set
  85  * @see     TreeSet
  86  * @see     HashMap
  87  * @since   1.2
  88  */
  89 
  90 public class HashSet<E>
  91     extends AbstractSet<E>
  92     implements Set<E>, Cloneable, java.io.Serializable
  93 {

  94     static final long serialVersionUID = -5024744406713321676L;
  95 
  96     private transient HashMap<E,Object> map;
  97 
  98     // Dummy value to associate with an Object in the backing Map
  99     private static final Object PRESENT = new Object();
 100 
 101     /**
 102      * Constructs a new, empty set; the backing {@code HashMap} instance has
 103      * default initial capacity (16) and load factor (0.75).
 104      */
 105     public HashSet() {
 106         map = new HashMap<>();
 107     }
 108 
 109     /**
 110      * Constructs a new set containing the elements in the specified
 111      * collection.  The {@code HashMap} is created with default load factor
 112      * (0.75) and an initial capacity sufficient to contain the elements in
 113      * the specified collection.


 254     public Object clone() {
 255         try {
 256             HashSet<E> newSet = (HashSet<E>) super.clone();
 257             newSet.map = (HashMap<E, Object>) map.clone();
 258             return newSet;
 259         } catch (CloneNotSupportedException e) {
 260             throw new InternalError(e);
 261         }
 262     }
 263 
 264     /**
 265      * Save the state of this {@code HashSet} instance to a stream (that is,
 266      * serialize it).
 267      *
 268      * @serialData The capacity of the backing {@code HashMap} instance
 269      *             (int), and its load factor (float) are emitted, followed by
 270      *             the size of the set (the number of elements it contains)
 271      *             (int), followed by all of its elements (each an Object) in
 272      *             no particular order.
 273      */

 274     private void writeObject(java.io.ObjectOutputStream s)
 275         throws java.io.IOException {
 276         // Write out any hidden serialization magic
 277         s.defaultWriteObject();
 278 
 279         // Write out HashMap capacity and load factor
 280         s.writeInt(map.capacity());
 281         s.writeFloat(map.loadFactor());
 282 
 283         // Write out size
 284         s.writeInt(map.size());
 285 
 286         // Write out all elements in the proper order.
 287         for (E e : map.keySet())
 288             s.writeObject(e);
 289     }
 290 
 291     /**
 292      * Reconstitute the {@code HashSet} instance from a stream (that is,
 293      * deserialize it).
 294      */

 295     private void readObject(java.io.ObjectInputStream s)
 296         throws java.io.IOException, ClassNotFoundException {
 297         // Read in any hidden serialization magic
 298         s.defaultReadObject();
 299 
 300         // Read capacity and verify non-negative.
 301         int capacity = s.readInt();
 302         if (capacity < 0) {
 303             throw new InvalidObjectException("Illegal capacity: " +
 304                                              capacity);
 305         }
 306 
 307         // Read load factor and verify positive and non NaN.
 308         float loadFactor = s.readFloat();
 309         if (loadFactor <= 0 || Float.isNaN(loadFactor)) {
 310             throw new InvalidObjectException("Illegal load factor: " +
 311                                              loadFactor);
 312         }
 313 
 314         // Read size and verify non-negative.




  74  *
  75  * <p>This class is a member of the
  76  * <a href="{@docRoot}/java.base/java/util/package-summary.html#CollectionsFramework">
  77  * Java Collections Framework</a>.
  78  *
  79  * @param <E> the type of elements maintained by this set
  80  *
  81  * @author  Josh Bloch
  82  * @author  Neal Gafter
  83  * @see     Collection
  84  * @see     Set
  85  * @see     TreeSet
  86  * @see     HashMap
  87  * @since   1.2
  88  */
  89 
  90 public class HashSet<E>
  91     extends AbstractSet<E>
  92     implements Set<E>, Cloneable, java.io.Serializable
  93 {
  94     @java.io.Serial
  95     static final long serialVersionUID = -5024744406713321676L;
  96 
  97     private transient HashMap<E,Object> map;
  98 
  99     // Dummy value to associate with an Object in the backing Map
 100     private static final Object PRESENT = new Object();
 101 
 102     /**
 103      * Constructs a new, empty set; the backing {@code HashMap} instance has
 104      * default initial capacity (16) and load factor (0.75).
 105      */
 106     public HashSet() {
 107         map = new HashMap<>();
 108     }
 109 
 110     /**
 111      * Constructs a new set containing the elements in the specified
 112      * collection.  The {@code HashMap} is created with default load factor
 113      * (0.75) and an initial capacity sufficient to contain the elements in
 114      * the specified collection.


 255     public Object clone() {
 256         try {
 257             HashSet<E> newSet = (HashSet<E>) super.clone();
 258             newSet.map = (HashMap<E, Object>) map.clone();
 259             return newSet;
 260         } catch (CloneNotSupportedException e) {
 261             throw new InternalError(e);
 262         }
 263     }
 264 
 265     /**
 266      * Save the state of this {@code HashSet} instance to a stream (that is,
 267      * serialize it).
 268      *
 269      * @serialData The capacity of the backing {@code HashMap} instance
 270      *             (int), and its load factor (float) are emitted, followed by
 271      *             the size of the set (the number of elements it contains)
 272      *             (int), followed by all of its elements (each an Object) in
 273      *             no particular order.
 274      */
 275     @java.io.Serial
 276     private void writeObject(java.io.ObjectOutputStream s)
 277         throws java.io.IOException {
 278         // Write out any hidden serialization magic
 279         s.defaultWriteObject();
 280 
 281         // Write out HashMap capacity and load factor
 282         s.writeInt(map.capacity());
 283         s.writeFloat(map.loadFactor());
 284 
 285         // Write out size
 286         s.writeInt(map.size());
 287 
 288         // Write out all elements in the proper order.
 289         for (E e : map.keySet())
 290             s.writeObject(e);
 291     }
 292 
 293     /**
 294      * Reconstitute the {@code HashSet} instance from a stream (that is,
 295      * deserialize it).
 296      */
 297     @java.io.Serial
 298     private void readObject(java.io.ObjectInputStream s)
 299         throws java.io.IOException, ClassNotFoundException {
 300         // Read in any hidden serialization magic
 301         s.defaultReadObject();
 302 
 303         // Read capacity and verify non-negative.
 304         int capacity = s.readInt();
 305         if (capacity < 0) {
 306             throw new InvalidObjectException("Illegal capacity: " +
 307                                              capacity);
 308         }
 309 
 310         // Read load factor and verify positive and non NaN.
 311         float loadFactor = s.readFloat();
 312         if (loadFactor <= 0 || Float.isNaN(loadFactor)) {
 313             throw new InvalidObjectException("Illegal load factor: " +
 314                                              loadFactor);
 315         }
 316 
 317         // Read size and verify non-negative.


< prev index next >