Class HashSet<E>
java.lang.Object
java.util.AbstractCollection<E>
java.util.AbstractSet<E>
java.util.HashSet<E>
- All Implemented Interfaces:
Iterable<E>, Collection<E>, Set<E>
- Direct Known Subclasses:
LinkedHashSet
HashSet is an implementation of a Set. All optional operations (adding and
removing) are supported. The elements can be any objects.
-
Constructor Summary
ConstructorsConstructorDescriptionHashSet()Constructs a new empty instance ofHashSet.HashSet(int capacity) Constructs a new instance ofHashSetwith the specified capacity.HashSet(int capacity, float loadFactor) Constructs a new instance ofHashSetwith the specified capacity and load factor.HashSet(Collection<? extends E> collection) Constructs a new instance ofHashSetcontaining the unique elements in the specified collection. -
Method Summary
Modifier and TypeMethodDescriptionbooleanAdds the specified object to thisHashSetif not already present.voidclear()Removes all elements from thisHashSet, leaving it empty.booleanSearches thisHashSetfor the specified object.booleanisEmpty()Returns true if thisHashSethas no elements, false otherwise.iterator()Returns an Iterator on the elements of thisHashSet.booleanRemoves the specified object from thisHashSet.intsize()Returns the number of elements in thisHashSet.Methods inherited from class AbstractSet
equals, hashCode, removeAllModifier and TypeMethodDescriptionbooleanCompares the specified object to this Set and returns true if they are equal.inthashCode()Returns the hash code for this set.booleanremoveAll(Collection<?> collection) Removes all occurrences in this collection which are contained in the specified collection.Methods inherited from class AbstractCollection
addAll, containsAll, retainAll, toArray, toArray, toStringModifier and TypeMethodDescriptionbooleanaddAll(Collection<? extends E> collection) Attempts to add all of the objects contained incollectionto the contents of thisCollection(optional).booleancontainsAll(Collection<?> collection) Tests whether thisCollectioncontains all objects contained in the specifiedCollection.booleanretainAll(Collection<?> collection) Removes all objects from thisCollectionthat are not also found in theCollectionpassed (optional).Object[]toArray()Returns a new array containing all elements contained in thisArrayList.<T> T[]toArray(T[] contents) Returns an array containing all elements contained in thisArrayList.toString()Returns the string representation of thisCollection.Methods inherited from class Object
clone, getClass, notify, notifyAll, wait, wait, waitModifier and TypeMethodDescriptionprotected Objectclone()final ClassgetClass()Returns the runtime class of an object.final voidnotify()Wakes up a single thread that is waiting on this object's monitor.final voidWakes up all threads that are waiting on this object's monitor.final voidwait()Causes current thread to wait until another thread invokes the method or the method for this object.final voidwait(long timeout) Causes current thread to wait until either another thread invokes the method or the method for this object, or a specified amount of time has elapsed.final voidwait(long timeout, int nanos) Causes current thread to wait until another thread invokes the method or the method for this object, or some other thread interrupts the current thread, or a certain amount of real time has elapsed.Methods inherited from interface Set
addAll, containsAll, retainAll, toArray, toArrayModifier and TypeMethodDescriptionbooleanaddAll(Collection<? extends E> collection) Adds the objects in the specified collection which do not exist yet in this set.booleancontainsAll(Collection<?> collection) Searches this set for all objects in the specified collection.booleanretainAll(Collection<?> collection) Removes all objects from this set that are not contained in the specified collection.Object[]toArray()Returns an array containing all elements contained in this set.<T> T[]toArray(T[] array) Returns an array containing all elements contained in this set.
-
Constructor Details
-
HashSet
public HashSet()Constructs a new empty instance ofHashSet. -
HashSet
public HashSet(int capacity) Constructs a new instance ofHashSetwith the specified capacity.- Parameters:
capacity- the initial capacity of thisHashSet.
-
HashSet
public HashSet(int capacity, float loadFactor) Constructs a new instance ofHashSetwith the specified capacity and load factor.- Parameters:
capacity- the initial capacity.loadFactor- the initial load factor.
-
HashSet
Constructs a new instance ofHashSetcontaining the unique elements in the specified collection.- Parameters:
collection- the collection of elements to add.
-
-
Method Details
-
add
Adds the specified object to thisHashSetif not already present.- Specified by:
addin interfaceCollection<E>- Specified by:
addin interfaceSet<E>- Overrides:
addin classAbstractCollection<E>- Parameters:
object- the object to add.- Returns:
truewhen thisHashSetdid not already contain the object,falseotherwise
-
clear
public void clear()Removes all elements from thisHashSet, leaving it empty.- Specified by:
clearin interfaceCollection<E>- Specified by:
clearin interfaceSet<E>- Overrides:
clearin classAbstractCollection<E>- See Also:
-
contains
Searches thisHashSetfor the specified object.- Specified by:
containsin interfaceCollection<E>- Specified by:
containsin interfaceSet<E>- Overrides:
containsin classAbstractCollection<E>- Parameters:
object- the object to search for.- Returns:
trueifobjectis an element of thisHashSet,falseotherwise.
-
isEmpty
public boolean isEmpty()Returns true if thisHashSethas no elements, false otherwise.- Specified by:
isEmptyin interfaceCollection<E>- Specified by:
isEmptyin interfaceSet<E>- Overrides:
isEmptyin classAbstractCollection<E>- Returns:
trueif thisHashSethas no elements,falseotherwise.- See Also:
-
iterator
Returns an Iterator on the elements of thisHashSet. -
remove
Removes the specified object from thisHashSet.- Specified by:
removein interfaceCollection<E>- Specified by:
removein interfaceSet<E>- Overrides:
removein classAbstractCollection<E>- Parameters:
object- the object to remove.- Returns:
trueif the object was removed,falseotherwise.
-
size
public int size()Returns the number of elements in thisHashSet.- Specified by:
sizein interfaceCollection<E>- Specified by:
sizein interfaceSet<E>- Specified by:
sizein classAbstractCollection<E>- Returns:
- the number of elements in this
HashSet.
-