Class HashSet<E>
- All Implemented Interfaces:
Iterable<E>, Collection<E>, Set<E>
- Direct Known Subclasses:
LinkedHashSet
-
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, removeAllMethods inherited from class AbstractCollection
addAll, containsAll, retainAll, toArray, toArray, toString
-
Constructor Details
-
HashSet
public HashSet()Constructs a new empty instance ofHashSet. -
HashSet
public HashSet(int capacity) Constructs a new instance of
HashSetwith the specified capacity.Parameters
capacity: the initial capacity of thisHashSet.
-
HashSet
public HashSet(int capacity, float loadFactor) Constructs a new instance of
HashSetwith the specified capacity and load factor.Parameters
-
capacity: the initial capacity. -
loadFactor: the initial load factor.
-
-
HashSet
Constructs a new instance of
HashSetcontaining the unique elements in the specified collection.Parameters
collection: the collection of elements to add.
-
-
Method Details
-
add
Adds the specified object to this
HashSetif not already present.Parameters
object: the object to add.
Returns
- Specified by:
addin interfaceCollection<E>- Specified by:
addin interfaceSet<E>- Overrides:
addin classAbstractCollection<E>- Returns:
truewhen thisHashSetdid not already contain the object,falseotherwise
-
clear
public void clear()Removes all elements from this
HashSet, leaving it empty.See also
-
#isEmpty
-
#size
- Specified by:
clearin interfaceCollection<E>- Specified by:
clearin interfaceSet<E>- Overrides:
clearin classAbstractCollection<E>
-
-
contains
Searches this
HashSetfor the specified object.Parameters
object: the object to search for.
Returns
- Specified by:
containsin interfaceCollection<E>- Specified by:
containsin interfaceSet<E>- Overrides:
containsin classAbstractCollection<E>- Returns:
trueifobjectis an element of thisHashSet,falseotherwise.
-
isEmpty
public boolean isEmpty()Returns true if this
HashSethas no elements, false otherwise.Returns
- Specified by:
isEmptyin interfaceCollection<E>- Specified by:
isEmptyin interfaceSet<E>- Overrides:
isEmptyin classAbstractCollection<E>- Returns:
trueif thisHashSethas no elements,falseotherwise.See also
- #size
-
iterator
Returns an Iterator on the elements of this
HashSet.Returns
an Iterator on the elements of this
HashSet.See also
- Iterator
-
remove
Removes the specified object from this
HashSet.Parameters
object: the object to remove.
Returns
trueif the object was removed,falseotherwise.- Specified by:
removein interfaceCollection<E>- Specified by:
removein interfaceSet<E>- Overrides:
removein classAbstractCollection<E>- Returns:
trueif thisCollectionis modified,falseotherwise.Throws
-
UnsupportedOperationException: if removing from thisCollectionis not supported. -
ClassCastException: if the object passed is not of the correct type. -
NullPointerException: @throws NullPointerException ifobjectisnulland thisCollectiondoesn't supportnullelements.
-
-
size
public int size()Returns the number of elements in this
HashSet.Returns
the number of elements in this
HashSet.- Specified by:
sizein interfaceCollection<E>- Specified by:
sizein interfaceSet<E>- Specified by:
sizein classAbstractCollection<E>- Returns:
- how many objects this
Collectioncontains, orInteger.MAX_VALUEif there are more thanInteger.MAX_VALUEelements in thisCollection.
-