jaroslav@557: /*
jaroslav@557: * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
jaroslav@557: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jaroslav@557: *
jaroslav@557: * This code is free software; you can redistribute it and/or modify it
jaroslav@557: * under the terms of the GNU General Public License version 2 only, as
jaroslav@557: * published by the Free Software Foundation. Oracle designates this
jaroslav@557: * particular file as subject to the "Classpath" exception as provided
jaroslav@557: * by Oracle in the LICENSE file that accompanied this code.
jaroslav@557: *
jaroslav@557: * This code is distributed in the hope that it will be useful, but WITHOUT
jaroslav@557: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jaroslav@557: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
jaroslav@557: * version 2 for more details (a copy is included in the LICENSE file that
jaroslav@557: * accompanied this code).
jaroslav@557: *
jaroslav@557: * You should have received a copy of the GNU General Public License version
jaroslav@557: * 2 along with this work; if not, write to the Free Software Foundation,
jaroslav@557: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jaroslav@557: *
jaroslav@557: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
jaroslav@557: * or visit www.oracle.com if you need additional information or have any
jaroslav@557: * questions.
jaroslav@557: */
jaroslav@557:
jaroslav@557: package java.util;
jaroslav@557:
jaroslav@557: /**
jaroslav@557: * This class implements the Set interface, backed by a hash table
jaroslav@557: * (actually a HashMap instance). It makes no guarantees as to the
jaroslav@557: * iteration order of the set; in particular, it does not guarantee that the
jaroslav@557: * order will remain constant over time. This class permits the null
jaroslav@557: * element.
jaroslav@557: *
jaroslav@557: *
This class offers constant time performance for the basic operations
jaroslav@557: * (add, remove, contains and size),
jaroslav@557: * assuming the hash function disperses the elements properly among the
jaroslav@557: * buckets. Iterating over this set requires time proportional to the sum of
jaroslav@557: * the HashSet instance's size (the number of elements) plus the
jaroslav@557: * "capacity" of the backing HashMap instance (the number of
jaroslav@557: * buckets). Thus, it's very important not to set the initial capacity too
jaroslav@557: * high (or the load factor too low) if iteration performance is important.
jaroslav@557: *
jaroslav@557: *
Note that this implementation is not synchronized.
jaroslav@557: * If multiple threads access a hash set concurrently, and at least one of
jaroslav@557: * the threads modifies the set, it must be synchronized externally.
jaroslav@557: * This is typically accomplished by synchronizing on some object that
jaroslav@557: * naturally encapsulates the set.
jaroslav@557: *
jaroslav@557: * If no such object exists, the set should be "wrapped" using the
jaroslav@557: * {@link Collections#synchronizedSet Collections.synchronizedSet}
jaroslav@557: * method. This is best done at creation time, to prevent accidental
jaroslav@557: * unsynchronized access to the set:
jaroslav@557: * Set s = Collections.synchronizedSet(new HashSet(...));
jaroslav@557: *
jaroslav@557: * The iterators returned by this class's iterator method are
jaroslav@557: * fail-fast: if the set is modified at any time after the iterator is
jaroslav@557: * created, in any way except through the iterator's own remove
jaroslav@557: * method, the Iterator throws a {@link ConcurrentModificationException}.
jaroslav@557: * Thus, in the face of concurrent modification, the iterator fails quickly
jaroslav@557: * and cleanly, rather than risking arbitrary, non-deterministic behavior at
jaroslav@557: * an undetermined time in the future.
jaroslav@557: *
jaroslav@557: *
Note that the fail-fast behavior of an iterator cannot be guaranteed
jaroslav@557: * as it is, generally speaking, impossible to make any hard guarantees in the
jaroslav@557: * presence of unsynchronized concurrent modification. Fail-fast iterators
jaroslav@557: * throw ConcurrentModificationException on a best-effort basis.
jaroslav@557: * Therefore, it would be wrong to write a program that depended on this
jaroslav@557: * exception for its correctness: the fail-fast behavior of iterators
jaroslav@557: * should be used only to detect bugs.
jaroslav@557: *
jaroslav@557: *
This class is a member of the
jaroslav@557: *
jaroslav@557: * Java Collections Framework.
jaroslav@557: *
jaroslav@557: * @param the type of elements maintained by this set
jaroslav@557: *
jaroslav@557: * @author Josh Bloch
jaroslav@557: * @author Neal Gafter
jaroslav@557: * @see Collection
jaroslav@557: * @see Set
jaroslav@557: * @see TreeSet
jaroslav@557: * @see HashMap
jaroslav@557: * @since 1.2
jaroslav@557: */
jaroslav@557:
jaroslav@557: public class HashSet
jaroslav@557: extends AbstractSet
jaroslav@557: implements Set, Cloneable, java.io.Serializable
jaroslav@557: {
jaroslav@557: static final long serialVersionUID = -5024744406713321676L;
jaroslav@557:
jaroslav@557: private transient HashMap map;
jaroslav@557:
jaroslav@557: // Dummy value to associate with an Object in the backing Map
jaroslav@557: private static final Object PRESENT = new Object();
jaroslav@557:
jaroslav@557: /**
jaroslav@557: * Constructs a new, empty set; the backing HashMap instance has
jaroslav@557: * default initial capacity (16) and load factor (0.75).
jaroslav@557: */
jaroslav@557: public HashSet() {
jaroslav@557: map = new HashMap<>();
jaroslav@557: }
jaroslav@557:
jaroslav@557: /**
jaroslav@557: * Constructs a new set containing the elements in the specified
jaroslav@557: * collection. The HashMap is created with default load factor
jaroslav@557: * (0.75) and an initial capacity sufficient to contain the elements in
jaroslav@557: * the specified collection.
jaroslav@557: *
jaroslav@557: * @param c the collection whose elements are to be placed into this set
jaroslav@557: * @throws NullPointerException if the specified collection is null
jaroslav@557: */
jaroslav@557: public HashSet(Collection extends E> c) {
jaroslav@557: map = new HashMap<>(Math.max((int) (c.size()/.75f) + 1, 16));
jaroslav@557: addAll(c);
jaroslav@557: }
jaroslav@557:
jaroslav@557: /**
jaroslav@557: * Constructs a new, empty set; the backing HashMap instance has
jaroslav@557: * the specified initial capacity and the specified load factor.
jaroslav@557: *
jaroslav@557: * @param initialCapacity the initial capacity of the hash map
jaroslav@557: * @param loadFactor the load factor of the hash map
jaroslav@557: * @throws IllegalArgumentException if the initial capacity is less
jaroslav@557: * than zero, or if the load factor is nonpositive
jaroslav@557: */
jaroslav@557: public HashSet(int initialCapacity, float loadFactor) {
jaroslav@557: map = new HashMap<>(initialCapacity, loadFactor);
jaroslav@557: }
jaroslav@557:
jaroslav@557: /**
jaroslav@557: * Constructs a new, empty set; the backing HashMap instance has
jaroslav@557: * the specified initial capacity and default load factor (0.75).
jaroslav@557: *
jaroslav@557: * @param initialCapacity the initial capacity of the hash table
jaroslav@557: * @throws IllegalArgumentException if the initial capacity is less
jaroslav@557: * than zero
jaroslav@557: */
jaroslav@557: public HashSet(int initialCapacity) {
jaroslav@557: map = new HashMap<>(initialCapacity);
jaroslav@557: }
jaroslav@557:
jaroslav@557: /**
jaroslav@557: * Constructs a new, empty linked hash set. (This package private
jaroslav@557: * constructor is only used by LinkedHashSet.) The backing
jaroslav@557: * HashMap instance is a LinkedHashMap with the specified initial
jaroslav@557: * capacity and the specified load factor.
jaroslav@557: *
jaroslav@557: * @param initialCapacity the initial capacity of the hash map
jaroslav@557: * @param loadFactor the load factor of the hash map
jaroslav@557: * @param dummy ignored (distinguishes this
jaroslav@557: * constructor from other int, float constructor.)
jaroslav@557: * @throws IllegalArgumentException if the initial capacity is less
jaroslav@557: * than zero, or if the load factor is nonpositive
jaroslav@557: */
jaroslav@557: HashSet(int initialCapacity, float loadFactor, boolean dummy) {
jaroslav@557: map = new LinkedHashMap<>(initialCapacity, loadFactor);
jaroslav@557: }
jaroslav@557:
jaroslav@557: /**
jaroslav@557: * Returns an iterator over the elements in this set. The elements
jaroslav@557: * are returned in no particular order.
jaroslav@557: *
jaroslav@557: * @return an Iterator over the elements in this set
jaroslav@557: * @see ConcurrentModificationException
jaroslav@557: */
jaroslav@557: public Iterator iterator() {
jaroslav@557: return map.keySet().iterator();
jaroslav@557: }
jaroslav@557:
jaroslav@557: /**
jaroslav@557: * Returns the number of elements in this set (its cardinality).
jaroslav@557: *
jaroslav@557: * @return the number of elements in this set (its cardinality)
jaroslav@557: */
jaroslav@557: public int size() {
jaroslav@557: return map.size();
jaroslav@557: }
jaroslav@557:
jaroslav@557: /**
jaroslav@557: * Returns true if this set contains no elements.
jaroslav@557: *
jaroslav@557: * @return true if this set contains no elements
jaroslav@557: */
jaroslav@557: public boolean isEmpty() {
jaroslav@557: return map.isEmpty();
jaroslav@557: }
jaroslav@557:
jaroslav@557: /**
jaroslav@557: * Returns true if this set contains the specified element.
jaroslav@557: * More formally, returns true if and only if this set
jaroslav@557: * contains an element e such that
jaroslav@557: * (o==null ? e==null : o.equals(e)).
jaroslav@557: *
jaroslav@557: * @param o element whose presence in this set is to be tested
jaroslav@557: * @return true if this set contains the specified element
jaroslav@557: */
jaroslav@557: public boolean contains(Object o) {
jaroslav@557: return map.containsKey(o);
jaroslav@557: }
jaroslav@557:
jaroslav@557: /**
jaroslav@557: * Adds the specified element to this set if it is not already present.
jaroslav@557: * More formally, adds the specified element e to this set if
jaroslav@557: * this set contains no element e2 such that
jaroslav@557: * (e==null ? e2==null : e.equals(e2)).
jaroslav@557: * If this set already contains the element, the call leaves the set
jaroslav@557: * unchanged and returns false.
jaroslav@557: *
jaroslav@557: * @param e element to be added to this set
jaroslav@557: * @return true if this set did not already contain the specified
jaroslav@557: * element
jaroslav@557: */
jaroslav@557: public boolean add(E e) {
jaroslav@557: return map.put(e, PRESENT)==null;
jaroslav@557: }
jaroslav@557:
jaroslav@557: /**
jaroslav@557: * Removes the specified element from this set if it is present.
jaroslav@557: * More formally, removes an element e such that
jaroslav@557: * (o==null ? e==null : o.equals(e)),
jaroslav@557: * if this set contains such an element. Returns true if
jaroslav@557: * this set contained the element (or equivalently, if this set
jaroslav@557: * changed as a result of the call). (This set will not contain the
jaroslav@557: * element once the call returns.)
jaroslav@557: *
jaroslav@557: * @param o object to be removed from this set, if present
jaroslav@557: * @return true if the set contained the specified element
jaroslav@557: */
jaroslav@557: public boolean remove(Object o) {
jaroslav@557: return map.remove(o)==PRESENT;
jaroslav@557: }
jaroslav@557:
jaroslav@557: /**
jaroslav@557: * Removes all of the elements from this set.
jaroslav@557: * The set will be empty after this call returns.
jaroslav@557: */
jaroslav@557: public void clear() {
jaroslav@557: map.clear();
jaroslav@557: }
jaroslav@557:
jaroslav@557: /**
jaroslav@557: * Returns a shallow copy of this HashSet instance: the elements
jaroslav@557: * themselves are not cloned.
jaroslav@557: *
jaroslav@557: * @return a shallow copy of this set
jaroslav@557: */
jaroslav@557: public Object clone() {
jaroslav@557: try {
jaroslav@557: HashSet newSet = (HashSet) super.clone();
jaroslav@557: newSet.map = (HashMap) map.clone();
jaroslav@557: return newSet;
jaroslav@557: } catch (CloneNotSupportedException e) {
jaroslav@557: throw new InternalError();
jaroslav@557: }
jaroslav@557: }
jaroslav@557:
jaroslav@557: }