Path: blob/master/src/java.base/share/classes/java/util/HashSet.java
41152 views
/*1* Copyright (c) 1997, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package java.util;2627import java.io.InvalidObjectException;28import jdk.internal.access.SharedSecrets;2930/**31* This class implements the {@code Set} interface, backed by a hash table32* (actually a {@code HashMap} instance). It makes no guarantees as to the33* iteration order of the set; in particular, it does not guarantee that the34* order will remain constant over time. This class permits the {@code null}35* element.36*37* <p>This class offers constant time performance for the basic operations38* ({@code add}, {@code remove}, {@code contains} and {@code size}),39* assuming the hash function disperses the elements properly among the40* buckets. Iterating over this set requires time proportional to the sum of41* the {@code HashSet} instance's size (the number of elements) plus the42* "capacity" of the backing {@code HashMap} instance (the number of43* buckets). Thus, it's very important not to set the initial capacity too44* high (or the load factor too low) if iteration performance is important.45*46* <p><strong>Note that this implementation is not synchronized.</strong>47* If multiple threads access a hash set concurrently, and at least one of48* the threads modifies the set, it <i>must</i> be synchronized externally.49* This is typically accomplished by synchronizing on some object that50* naturally encapsulates the set.51*52* If no such object exists, the set should be "wrapped" using the53* {@link Collections#synchronizedSet Collections.synchronizedSet}54* method. This is best done at creation time, to prevent accidental55* unsynchronized access to the set:<pre>56* Set s = Collections.synchronizedSet(new HashSet(...));</pre>57*58* <p>The iterators returned by this class's {@code iterator} method are59* <i>fail-fast</i>: if the set is modified at any time after the iterator is60* created, in any way except through the iterator's own {@code remove}61* method, the Iterator throws a {@link ConcurrentModificationException}.62* Thus, in the face of concurrent modification, the iterator fails quickly63* and cleanly, rather than risking arbitrary, non-deterministic behavior at64* an undetermined time in the future.65*66* <p>Note that the fail-fast behavior of an iterator cannot be guaranteed67* as it is, generally speaking, impossible to make any hard guarantees in the68* presence of unsynchronized concurrent modification. Fail-fast iterators69* throw {@code ConcurrentModificationException} on a best-effort basis.70* Therefore, it would be wrong to write a program that depended on this71* exception for its correctness: <i>the fail-fast behavior of iterators72* should be used only to detect bugs.</i>73*74* <p>This class is a member of the75* <a href="{@docRoot}/java.base/java/util/package-summary.html#CollectionsFramework">76* Java Collections Framework</a>.77*78* @param <E> the type of elements maintained by this set79*80* @author Josh Bloch81* @author Neal Gafter82* @see Collection83* @see Set84* @see TreeSet85* @see HashMap86* @since 1.287*/8889public class HashSet<E>90extends AbstractSet<E>91implements Set<E>, Cloneable, java.io.Serializable92{93@java.io.Serial94static final long serialVersionUID = -5024744406713321676L;9596private transient HashMap<E,Object> map;9798// Dummy value to associate with an Object in the backing Map99private static final Object PRESENT = new Object();100101/**102* Constructs a new, empty set; the backing {@code HashMap} instance has103* default initial capacity (16) and load factor (0.75).104*/105public HashSet() {106map = new HashMap<>();107}108109/**110* Constructs a new set containing the elements in the specified111* collection. The {@code HashMap} is created with default load factor112* (0.75) and an initial capacity sufficient to contain the elements in113* the specified collection.114*115* @param c the collection whose elements are to be placed into this set116* @throws NullPointerException if the specified collection is null117*/118public HashSet(Collection<? extends E> c) {119map = new HashMap<>(Math.max((int) (c.size()/.75f) + 1, 16));120addAll(c);121}122123/**124* Constructs a new, empty set; the backing {@code HashMap} instance has125* the specified initial capacity and the specified load factor.126*127* @param initialCapacity the initial capacity of the hash map128* @param loadFactor the load factor of the hash map129* @throws IllegalArgumentException if the initial capacity is less130* than zero, or if the load factor is nonpositive131*/132public HashSet(int initialCapacity, float loadFactor) {133map = new HashMap<>(initialCapacity, loadFactor);134}135136/**137* Constructs a new, empty set; the backing {@code HashMap} instance has138* the specified initial capacity and default load factor (0.75).139*140* @param initialCapacity the initial capacity of the hash table141* @throws IllegalArgumentException if the initial capacity is less142* than zero143*/144public HashSet(int initialCapacity) {145map = new HashMap<>(initialCapacity);146}147148/**149* Constructs a new, empty linked hash set. (This package private150* constructor is only used by LinkedHashSet.) The backing151* HashMap instance is a LinkedHashMap with the specified initial152* capacity and the specified load factor.153*154* @param initialCapacity the initial capacity of the hash map155* @param loadFactor the load factor of the hash map156* @param dummy ignored (distinguishes this157* constructor from other int, float constructor.)158* @throws IllegalArgumentException if the initial capacity is less159* than zero, or if the load factor is nonpositive160*/161HashSet(int initialCapacity, float loadFactor, boolean dummy) {162map = new LinkedHashMap<>(initialCapacity, loadFactor);163}164165/**166* Returns an iterator over the elements in this set. The elements167* are returned in no particular order.168*169* @return an Iterator over the elements in this set170* @see ConcurrentModificationException171*/172public Iterator<E> iterator() {173return map.keySet().iterator();174}175176/**177* Returns the number of elements in this set (its cardinality).178*179* @return the number of elements in this set (its cardinality)180*/181public int size() {182return map.size();183}184185/**186* Returns {@code true} if this set contains no elements.187*188* @return {@code true} if this set contains no elements189*/190public boolean isEmpty() {191return map.isEmpty();192}193194/**195* Returns {@code true} if this set contains the specified element.196* More formally, returns {@code true} if and only if this set197* contains an element {@code e} such that198* {@code Objects.equals(o, e)}.199*200* @param o element whose presence in this set is to be tested201* @return {@code true} if this set contains the specified element202*/203public boolean contains(Object o) {204return map.containsKey(o);205}206207/**208* Adds the specified element to this set if it is not already present.209* More formally, adds the specified element {@code e} to this set if210* this set contains no element {@code e2} such that211* {@code Objects.equals(e, e2)}.212* If this set already contains the element, the call leaves the set213* unchanged and returns {@code false}.214*215* @param e element to be added to this set216* @return {@code true} if this set did not already contain the specified217* element218*/219public boolean add(E e) {220return map.put(e, PRESENT)==null;221}222223/**224* Removes the specified element from this set if it is present.225* More formally, removes an element {@code e} such that226* {@code Objects.equals(o, e)},227* if this set contains such an element. Returns {@code true} if228* this set contained the element (or equivalently, if this set229* changed as a result of the call). (This set will not contain the230* element once the call returns.)231*232* @param o object to be removed from this set, if present233* @return {@code true} if the set contained the specified element234*/235public boolean remove(Object o) {236return map.remove(o)==PRESENT;237}238239/**240* Removes all of the elements from this set.241* The set will be empty after this call returns.242*/243public void clear() {244map.clear();245}246247/**248* Returns a shallow copy of this {@code HashSet} instance: the elements249* themselves are not cloned.250*251* @return a shallow copy of this set252*/253@SuppressWarnings("unchecked")254public Object clone() {255try {256HashSet<E> newSet = (HashSet<E>) super.clone();257newSet.map = (HashMap<E, Object>) map.clone();258return newSet;259} catch (CloneNotSupportedException e) {260throw new InternalError(e);261}262}263264/**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} instance269* (int), and its load factor (float) are emitted, followed by270* the size of the set (the number of elements it contains)271* (int), followed by all of its elements (each an Object) in272* no particular order.273*/274@java.io.Serial275private void writeObject(java.io.ObjectOutputStream s)276throws java.io.IOException {277// Write out any hidden serialization magic278s.defaultWriteObject();279280// Write out HashMap capacity and load factor281s.writeInt(map.capacity());282s.writeFloat(map.loadFactor());283284// Write out size285s.writeInt(map.size());286287// Write out all elements in the proper order.288for (E e : map.keySet())289s.writeObject(e);290}291292/**293* Reconstitute the {@code HashSet} instance from a stream (that is,294* deserialize it).295*/296@java.io.Serial297private void readObject(java.io.ObjectInputStream s)298throws java.io.IOException, ClassNotFoundException {299// Read in any hidden serialization magic300s.defaultReadObject();301302// Read capacity and verify non-negative.303int capacity = s.readInt();304if (capacity < 0) {305throw new InvalidObjectException("Illegal capacity: " +306capacity);307}308309// Read load factor and verify positive and non NaN.310float loadFactor = s.readFloat();311if (loadFactor <= 0 || Float.isNaN(loadFactor)) {312throw new InvalidObjectException("Illegal load factor: " +313loadFactor);314}315316// Read size and verify non-negative.317int size = s.readInt();318if (size < 0) {319throw new InvalidObjectException("Illegal size: " +320size);321}322323// Set the capacity according to the size and load factor ensuring that324// the HashMap is at least 25% full but clamping to maximum capacity.325capacity = (int) Math.min(size * Math.min(1 / loadFactor, 4.0f),326HashMap.MAXIMUM_CAPACITY);327328// Constructing the backing map will lazily create an array when the first element is329// added, so check it before construction. Call HashMap.tableSizeFor to compute the330// actual allocation size. Check Map.Entry[].class since it's the nearest public type to331// what is actually created.332SharedSecrets.getJavaObjectInputStreamAccess()333.checkArray(s, Map.Entry[].class, HashMap.tableSizeFor(capacity));334335// Create backing HashMap336map = (((HashSet<?>)this) instanceof LinkedHashSet ?337new LinkedHashMap<>(capacity, loadFactor) :338new HashMap<>(capacity, loadFactor));339340// Read in all elements in the proper order.341for (int i=0; i<size; i++) {342@SuppressWarnings("unchecked")343E e = (E) s.readObject();344map.put(e, PRESENT);345}346}347348/**349* Creates a <em><a href="Spliterator.html#binding">late-binding</a></em>350* and <em>fail-fast</em> {@link Spliterator} over the elements in this351* set.352*353* <p>The {@code Spliterator} reports {@link Spliterator#SIZED} and354* {@link Spliterator#DISTINCT}. Overriding implementations should document355* the reporting of additional characteristic values.356*357* @return a {@code Spliterator} over the elements in this set358* @since 1.8359*/360public Spliterator<E> spliterator() {361return new HashMap.KeySpliterator<>(map, 0, -1, 0, 0);362}363364@Override365public Object[] toArray() {366return map.keysToArray(new Object[map.size()]);367}368369@Override370public <T> T[] toArray(T[] a) {371return map.keysToArray(map.prepareArray(a));372}373}374375376