jdk/src/share/classes/java/util/HashSet.java
author psandoz
Fri, 20 Dec 2013 13:38:13 +0100
changeset 22078 bdec5d53e98c
parent 20750 330bf22c39fa
permissions -rw-r--r--
8030851: Update code in java.util to use newer language features Reviewed-by: dfuchs, briangoetz, chegar, alanb, mduigou
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
     2
 * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4110
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4110
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4110
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4110
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4110
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.util;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
20750
330bf22c39fa 8016252: More defensive HashSet.readObject
bpb
parents: 19435
diff changeset
    28
import java.io.InvalidObjectException;
330bf22c39fa 8016252: More defensive HashSet.readObject
bpb
parents: 19435
diff changeset
    29
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * This class implements the <tt>Set</tt> interface, backed by a hash table
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * (actually a <tt>HashMap</tt> instance).  It makes no guarantees as to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * iteration order of the set; in particular, it does not guarantee that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * order will remain constant over time.  This class permits the <tt>null</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * <p>This class offers constant time performance for the basic operations
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * (<tt>add</tt>, <tt>remove</tt>, <tt>contains</tt> and <tt>size</tt>),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * assuming the hash function disperses the elements properly among the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * buckets.  Iterating over this set requires time proportional to the sum of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * the <tt>HashSet</tt> instance's size (the number of elements) plus the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * "capacity" of the backing <tt>HashMap</tt> instance (the number of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * buckets).  Thus, it's very important not to set the initial capacity too
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * high (or the load factor too low) if iteration performance is important.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * <p><strong>Note that this implementation is not synchronized.</strong>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * If multiple threads access a hash set concurrently, and at least one of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * the threads modifies the set, it <i>must</i> be synchronized externally.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * This is typically accomplished by synchronizing on some object that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * naturally encapsulates the set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * If no such object exists, the set should be "wrapped" using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * {@link Collections#synchronizedSet Collections.synchronizedSet}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * method.  This is best done at creation time, to prevent accidental
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * unsynchronized access to the set:<pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *   Set s = Collections.synchronizedSet(new HashSet(...));</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * <p>The iterators returned by this class's <tt>iterator</tt> method are
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * <i>fail-fast</i>: if the set is modified at any time after the iterator is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * created, in any way except through the iterator's own <tt>remove</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * method, the Iterator throws a {@link ConcurrentModificationException}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * Thus, in the face of concurrent modification, the iterator fails quickly
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * and cleanly, rather than risking arbitrary, non-deterministic behavior at
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * an undetermined time in the future.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * <p>Note that the fail-fast behavior of an iterator cannot be guaranteed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * as it is, generally speaking, impossible to make any hard guarantees in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * presence of unsynchronized concurrent modification.  Fail-fast iterators
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * throw <tt>ConcurrentModificationException</tt> on a best-effort basis.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * Therefore, it would be wrong to write a program that depended on this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * exception for its correctness: <i>the fail-fast behavior of iterators
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * should be used only to detect bugs.</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * <p>This class is a member of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * <a href="{@docRoot}/../technotes/guides/collections/index.html">
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * Java Collections Framework</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * @param <E> the type of elements maintained by this set
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * @author  Josh Bloch
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * @author  Neal Gafter
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * @see     Collection
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * @see     Set
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * @see     TreeSet
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * @see     HashMap
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * @since   1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
public class HashSet<E>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    extends AbstractSet<E>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    implements Set<E>, Cloneable, java.io.Serializable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    static final long serialVersionUID = -5024744406713321676L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    private transient HashMap<E,Object> map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    // Dummy value to associate with an Object in the backing Map
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    private static final Object PRESENT = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * Constructs a new, empty set; the backing <tt>HashMap</tt> instance has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * default initial capacity (16) and load factor (0.75).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    public HashSet() {
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 5506
diff changeset
   105
        map = new HashMap<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * Constructs a new set containing the elements in the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * collection.  The <tt>HashMap</tt> is created with default load factor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * (0.75) and an initial capacity sufficient to contain the elements in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * the specified collection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * @param c the collection whose elements are to be placed into this set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * @throws NullPointerException if the specified collection is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    public HashSet(Collection<? extends E> c) {
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 5506
diff changeset
   118
        map = new HashMap<>(Math.max((int) (c.size()/.75f) + 1, 16));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        addAll(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * Constructs a new, empty set; the backing <tt>HashMap</tt> instance has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * the specified initial capacity and the specified load factor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * @param      initialCapacity   the initial capacity of the hash map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * @param      loadFactor        the load factor of the hash map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * @throws     IllegalArgumentException if the initial capacity is less
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     *             than zero, or if the load factor is nonpositive
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    public HashSet(int initialCapacity, float loadFactor) {
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 5506
diff changeset
   132
        map = new HashMap<>(initialCapacity, loadFactor);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * Constructs a new, empty set; the backing <tt>HashMap</tt> instance has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * the specified initial capacity and default load factor (0.75).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * @param      initialCapacity   the initial capacity of the hash table
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * @throws     IllegalArgumentException if the initial capacity is less
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     *             than zero
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    public HashSet(int initialCapacity) {
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 5506
diff changeset
   144
        map = new HashMap<>(initialCapacity);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * Constructs a new, empty linked hash set.  (This package private
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * constructor is only used by LinkedHashSet.) The backing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * HashMap instance is a LinkedHashMap with the specified initial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * capacity and the specified load factor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * @param      initialCapacity   the initial capacity of the hash map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * @param      loadFactor        the load factor of the hash map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * @param      dummy             ignored (distinguishes this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     *             constructor from other int, float constructor.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * @throws     IllegalArgumentException if the initial capacity is less
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     *             than zero, or if the load factor is nonpositive
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    HashSet(int initialCapacity, float loadFactor, boolean dummy) {
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 5506
diff changeset
   161
        map = new LinkedHashMap<>(initialCapacity, loadFactor);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * Returns an iterator over the elements in this set.  The elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * are returned in no particular order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * @return an Iterator over the elements in this set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * @see ConcurrentModificationException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    public Iterator<E> iterator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        return map.keySet().iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * Returns the number of elements in this set (its cardinality).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * @return the number of elements in this set (its cardinality)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    public int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        return map.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * Returns <tt>true</tt> if this set contains no elements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * @return <tt>true</tt> if this set contains no elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    public boolean isEmpty() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        return map.isEmpty();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * Returns <tt>true</tt> if this set contains the specified element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * More formally, returns <tt>true</tt> if and only if this set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * contains an element <tt>e</tt> such that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * <tt>(o==null&nbsp;?&nbsp;e==null&nbsp;:&nbsp;o.equals(e))</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * @param o element whose presence in this set is to be tested
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * @return <tt>true</tt> if this set contains the specified element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    public boolean contains(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        return map.containsKey(o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * Adds the specified element to this set if it is not already present.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * More formally, adds the specified element <tt>e</tt> to this set if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * this set contains no element <tt>e2</tt> such that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * <tt>(e==null&nbsp;?&nbsp;e2==null&nbsp;:&nbsp;e.equals(e2))</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * If this set already contains the element, the call leaves the set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * unchanged and returns <tt>false</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * @param e element to be added to this set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * @return <tt>true</tt> if this set did not already contain the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    public boolean add(E e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        return map.put(e, PRESENT)==null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * Removes the specified element from this set if it is present.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * More formally, removes an element <tt>e</tt> such that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * <tt>(o==null&nbsp;?&nbsp;e==null&nbsp;:&nbsp;o.equals(e))</tt>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * if this set contains such an element.  Returns <tt>true</tt> if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * this set contained the element (or equivalently, if this set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * changed as a result of the call).  (This set will not contain the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * element once the call returns.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * @param o object to be removed from this set, if present
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * @return <tt>true</tt> if the set contained the specified element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    public boolean remove(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        return map.remove(o)==PRESENT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * Removes all of the elements from this set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * The set will be empty after this call returns.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    public void clear() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        map.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * Returns a shallow copy of this <tt>HashSet</tt> instance: the elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * themselves are not cloned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * @return a shallow copy of this set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     */
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 10419
diff changeset
   252
    @SuppressWarnings("unchecked")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    public Object clone() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            HashSet<E> newSet = (HashSet<E>) super.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            newSet.map = (HashMap<E, Object>) map.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            return newSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        } catch (CloneNotSupportedException e) {
10419
12c063b39232 7084245: Update usages of InternalError to use exception chaining
sherman
parents: 9035
diff changeset
   259
            throw new InternalError(e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * Save the state of this <tt>HashSet</tt> instance to a stream (that is,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * serialize it).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * @serialData The capacity of the backing <tt>HashMap</tt> instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     *             (int), and its load factor (float) are emitted, followed by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     *             the size of the set (the number of elements it contains)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     *             (int), followed by all of its elements (each an Object) in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     *             no particular order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    private void writeObject(java.io.ObjectOutputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        throws java.io.IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        // Write out any hidden serialization magic
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        s.defaultWriteObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        // Write out HashMap capacity and load factor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        s.writeInt(map.capacity());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        s.writeFloat(map.loadFactor());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        // Write out size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        s.writeInt(map.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        // Write out all elements in the proper order.
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents: 2
diff changeset
   286
        for (E e : map.keySet())
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents: 2
diff changeset
   287
            s.writeObject(e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * Reconstitute the <tt>HashSet</tt> instance from a stream (that is,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * deserialize it).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    private void readObject(java.io.ObjectInputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        throws java.io.IOException, ClassNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        // Read in any hidden serialization magic
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        s.defaultReadObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
20750
330bf22c39fa 8016252: More defensive HashSet.readObject
bpb
parents: 19435
diff changeset
   299
        // Read capacity and verify non-negative.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        int capacity = s.readInt();
20750
330bf22c39fa 8016252: More defensive HashSet.readObject
bpb
parents: 19435
diff changeset
   301
        if (capacity < 0) {
330bf22c39fa 8016252: More defensive HashSet.readObject
bpb
parents: 19435
diff changeset
   302
            throw new InvalidObjectException("Illegal capacity: " +
330bf22c39fa 8016252: More defensive HashSet.readObject
bpb
parents: 19435
diff changeset
   303
                                             capacity);
330bf22c39fa 8016252: More defensive HashSet.readObject
bpb
parents: 19435
diff changeset
   304
        }
330bf22c39fa 8016252: More defensive HashSet.readObject
bpb
parents: 19435
diff changeset
   305
330bf22c39fa 8016252: More defensive HashSet.readObject
bpb
parents: 19435
diff changeset
   306
        // Read load factor and verify positive and non NaN.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        float loadFactor = s.readFloat();
20750
330bf22c39fa 8016252: More defensive HashSet.readObject
bpb
parents: 19435
diff changeset
   308
        if (loadFactor <= 0 || Float.isNaN(loadFactor)) {
330bf22c39fa 8016252: More defensive HashSet.readObject
bpb
parents: 19435
diff changeset
   309
            throw new InvalidObjectException("Illegal load factor: " +
330bf22c39fa 8016252: More defensive HashSet.readObject
bpb
parents: 19435
diff changeset
   310
                                             loadFactor);
330bf22c39fa 8016252: More defensive HashSet.readObject
bpb
parents: 19435
diff changeset
   311
        }
330bf22c39fa 8016252: More defensive HashSet.readObject
bpb
parents: 19435
diff changeset
   312
330bf22c39fa 8016252: More defensive HashSet.readObject
bpb
parents: 19435
diff changeset
   313
        // Read size and verify non-negative.
330bf22c39fa 8016252: More defensive HashSet.readObject
bpb
parents: 19435
diff changeset
   314
        int size = s.readInt();
330bf22c39fa 8016252: More defensive HashSet.readObject
bpb
parents: 19435
diff changeset
   315
        if (size < 0) {
330bf22c39fa 8016252: More defensive HashSet.readObject
bpb
parents: 19435
diff changeset
   316
            throw new InvalidObjectException("Illegal size: " +
330bf22c39fa 8016252: More defensive HashSet.readObject
bpb
parents: 19435
diff changeset
   317
                                             size);
330bf22c39fa 8016252: More defensive HashSet.readObject
bpb
parents: 19435
diff changeset
   318
        }
330bf22c39fa 8016252: More defensive HashSet.readObject
bpb
parents: 19435
diff changeset
   319
330bf22c39fa 8016252: More defensive HashSet.readObject
bpb
parents: 19435
diff changeset
   320
        // Set the capacity according to the size and load factor ensuring that
330bf22c39fa 8016252: More defensive HashSet.readObject
bpb
parents: 19435
diff changeset
   321
        // the HashMap is at least 25% full but clamping to maximum capacity.
330bf22c39fa 8016252: More defensive HashSet.readObject
bpb
parents: 19435
diff changeset
   322
        capacity = (int) Math.min(size * Math.min(1 / loadFactor, 4.0f),
330bf22c39fa 8016252: More defensive HashSet.readObject
bpb
parents: 19435
diff changeset
   323
                HashMap.MAXIMUM_CAPACITY);
330bf22c39fa 8016252: More defensive HashSet.readObject
bpb
parents: 19435
diff changeset
   324
330bf22c39fa 8016252: More defensive HashSet.readObject
bpb
parents: 19435
diff changeset
   325
        // Create backing HashMap
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 10419
diff changeset
   326
        map = (((HashSet<?>)this) instanceof LinkedHashSet ?
22078
bdec5d53e98c 8030851: Update code in java.util to use newer language features
psandoz
parents: 20750
diff changeset
   327
               new LinkedHashMap<>(capacity, loadFactor) :
bdec5d53e98c 8030851: Update code in java.util to use newer language features
psandoz
parents: 20750
diff changeset
   328
               new HashMap<>(capacity, loadFactor));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        // Read in all elements in the proper order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        for (int i=0; i<size; i++) {
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 10419
diff changeset
   332
            @SuppressWarnings("unchecked")
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 10419
diff changeset
   333
                E e = (E) s.readObject();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            map.put(e, PRESENT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    }
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
   337
19435
9d7530ff42cb 8014824: Document Spliterator characteristics and binding policy of java util collection impls
psandoz
parents: 17168
diff changeset
   338
    /**
9d7530ff42cb 8014824: Document Spliterator characteristics and binding policy of java util collection impls
psandoz
parents: 17168
diff changeset
   339
     * Creates a <em><a href="Spliterator.html#binding">late-binding</a></em>
9d7530ff42cb 8014824: Document Spliterator characteristics and binding policy of java util collection impls
psandoz
parents: 17168
diff changeset
   340
     * and <em>fail-fast</em> {@link Spliterator} over the elements in this
9d7530ff42cb 8014824: Document Spliterator characteristics and binding policy of java util collection impls
psandoz
parents: 17168
diff changeset
   341
     * set.
9d7530ff42cb 8014824: Document Spliterator characteristics and binding policy of java util collection impls
psandoz
parents: 17168
diff changeset
   342
     *
9d7530ff42cb 8014824: Document Spliterator characteristics and binding policy of java util collection impls
psandoz
parents: 17168
diff changeset
   343
     * <p>The {@code Spliterator} reports {@link Spliterator#SIZED} and
9d7530ff42cb 8014824: Document Spliterator characteristics and binding policy of java util collection impls
psandoz
parents: 17168
diff changeset
   344
     * {@link Spliterator#DISTINCT}.  Overriding implementations should document
9d7530ff42cb 8014824: Document Spliterator characteristics and binding policy of java util collection impls
psandoz
parents: 17168
diff changeset
   345
     * the reporting of additional characteristic values.
9d7530ff42cb 8014824: Document Spliterator characteristics and binding policy of java util collection impls
psandoz
parents: 17168
diff changeset
   346
     *
9d7530ff42cb 8014824: Document Spliterator characteristics and binding policy of java util collection impls
psandoz
parents: 17168
diff changeset
   347
     * @return a {@code Spliterator} over the elements in this set
9d7530ff42cb 8014824: Document Spliterator characteristics and binding policy of java util collection impls
psandoz
parents: 17168
diff changeset
   348
     * @since 1.8
9d7530ff42cb 8014824: Document Spliterator characteristics and binding policy of java util collection impls
psandoz
parents: 17168
diff changeset
   349
     */
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
   350
    public Spliterator<E> spliterator() {
22078
bdec5d53e98c 8030851: Update code in java.util to use newer language features
psandoz
parents: 20750
diff changeset
   351
        return new HashMap.KeySpliterator<>(map, 0, -1, 0, 0);
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
   352
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
}