Constructors Summary |
---|
public HashSet()Constructs a new empty instance of {@code HashSet}.
this(new HashMap<E, HashSet<E>>());
|
public HashSet(int capacity)Constructs a new instance of {@code HashSet} with the specified capacity.
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.
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.
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 boolean | add(E object)Adds the specified object to this {@code HashSet} if not already present.
return backingMap.put(object, this) == null;
|
public void | clear()Removes all elements from this {@code HashSet}, leaving it empty.
backingMap.clear();
|
public java.lang.Object | clone()Returns a new {@code HashSet} with the same elements and size as this
{@code HashSet}.
try {
HashSet<E> clone = (HashSet<E>) super.clone();
clone.backingMap = (HashMap<E, HashSet<E>>) backingMap.clone();
return clone;
} catch (CloneNotSupportedException e) {
return null;
}
|
public boolean | contains(java.lang.Object object)Searches this {@code HashSet} for the specified object.
return backingMap.containsKey(object);
|
java.util.HashMap | createBackingMap(int capacity, float loadFactor)
return new HashMap<E, HashSet<E>>(capacity, loadFactor);
|
public boolean | isEmpty()Returns true if this {@code HashSet} has no elements, false otherwise.
return backingMap.isEmpty();
|
public java.util.Iterator | iterator()Returns an Iterator on the elements of this {@code HashSet}.
return backingMap.keySet().iterator();
|
private void | readObject(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 boolean | remove(java.lang.Object object)Removes the specified object from this {@code HashSet}.
return backingMap.remove(object) != null;
|
public int | size()Returns the number of elements in this {@code HashSet}.
return backingMap.size();
|
private void | writeObject(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;
}
}
|