FileDocCategorySizeDatePackage
HashSet.javaAPI DocAndroid 1.5 API6996Wed May 06 22:41:04 BST 2009java.util

HashSet

public class HashSet extends AbstractSet implements Serializable, Set, Cloneable
HashSet is an implementation of a Set. All optional operations (adding and removing) are supported. The elements can be any objects.
since
Android 1.0

Fields Summary
private static final long
serialVersionUID
transient HashMap
backingMap
Constructors Summary
public HashSet()
Constructs a new empty instance of {@code HashSet}.

since
Android 1.0


                     
      
        this(new HashMap<E, HashSet<E>>());
    
public HashSet(int capacity)
Constructs a new instance of {@code HashSet} with the specified capacity.

param
capacity the initial capacity of this {@code HashSet}.
since
Android 1.0

        this(new HashMap<E, HashSet<E>>(capacity));
    
public HashSet(int capacity, float loadFactor)
Constructs a new instance of {@code HashSet} with the specified capacity and load factor.

param
capacity the initial capacity.
param
loadFactor the initial load factor.
since
Android 1.0

        this(new HashMap<E, HashSet<E>>(capacity, loadFactor));
    
public HashSet(Collection collection)
Constructs a new instance of {@code HashSet} containing the unique elements in the specified collection.

param
collection the collection of elements to add.
since
Android 1.0

        this(new HashMap<E, HashSet<E>>(collection.size() < 6 ? 11 : collection.size() * 2));
        for (E e : collection) {
            add(e);
        }
    
HashSet(HashMap backingMap)

        this.backingMap = backingMap;
    
Methods Summary
public booleanadd(E object)
Adds the specified object to this {@code HashSet} if not already present.

param
object the object to add.
return
{@code true} when this {@code HashSet} did not already contain the object, {@code false} otherwise
since
Android 1.0

        return backingMap.put(object, this) == null;
    
public voidclear()
Removes all elements from this {@code HashSet}, leaving it empty.

see
#isEmpty
see
#size
since
Android 1.0

        backingMap.clear();
    
public java.lang.Objectclone()
Returns a new {@code HashSet} with the same elements and size as this {@code HashSet}.

return
a shallow copy of this {@code HashSet}.
see
java.lang.Cloneable
since
Android 1.0

        try {
            HashSet<E> clone = (HashSet<E>) super.clone();
            clone.backingMap = (HashMap<E, HashSet<E>>) backingMap.clone();
            return clone;
        } catch (CloneNotSupportedException e) {
            return null;
        }
    
public booleancontains(java.lang.Object object)
Searches this {@code HashSet} for the specified object.

param
object the object to search for.
return
{@code true} if {@code object} is an element of this {@code HashSet}, {@code false} otherwise.
since
Android 1.0

        return backingMap.containsKey(object);
    
java.util.HashMapcreateBackingMap(int capacity, float loadFactor)

        return new HashMap<E, HashSet<E>>(capacity, loadFactor);
    
public booleanisEmpty()
Returns true if this {@code HashSet} has no elements, false otherwise.

return
{@code true} if this {@code HashSet} has no elements, {@code false} otherwise.
see
#size
since
Android 1.0

        return backingMap.isEmpty();
    
public java.util.Iteratoriterator()
Returns an Iterator on the elements of this {@code HashSet}.

return
an Iterator on the elements of this {@code HashSet}.
see
Iterator
since
Android 1.0

        return backingMap.keySet().iterator();
    
private voidreadObject(java.io.ObjectInputStream stream)

        stream.defaultReadObject();
        int length = stream.readInt();
        float loadFactor = stream.readFloat();
        backingMap = createBackingMap(length, loadFactor);
        int elementCount = stream.readInt();
        for (int i = elementCount; --i >= 0;) {
            E key = (E)stream.readObject();
            backingMap.put(key, this);
        }
    
public booleanremove(java.lang.Object object)
Removes the specified object from this {@code HashSet}.

param
object the object to remove.
return
{@code true} if the object was removed, {@code false} otherwise.
since
Android 1.0

        return backingMap.remove(object) != null;
    
public intsize()
Returns the number of elements in this {@code HashSet}.

return
the number of elements in this {@code HashSet}.
since
Android 1.0

        return backingMap.size();
    
private voidwriteObject(java.io.ObjectOutputStream stream)

        stream.defaultWriteObject();
        stream.writeInt(backingMap.elementData.length);
        stream.writeFloat(backingMap.loadFactor);
        stream.writeInt(backingMap.elementCount);
        for (int i = backingMap.elementData.length; --i >= 0;) {
            HashMap.Entry<E, HashSet<E>> entry = backingMap.elementData[i];
            while (entry != null) {
                stream.writeObject(entry.key);
                entry = entry.next;
            }
        }