Package java.util
Class HashSet<E>
java.lang.Object
java.util.AbstractCollection<E>
java.util.AbstractSet<E>
java.util.HashSet<E>
- All Implemented Interfaces:
Serializable,Cloneable,Iterable<E>,Collection<E>,Set<E>
- Direct Known Subclasses:
LinkedHashSet
public class HashSet<E> extends AbstractSet<E> implements Set<E>, Cloneable, Serializable
HashSet is an implementation of a Set. All optional operations (adding and
removing) are supported. The elements can be any objects.
- See Also:
- Serialized Form
-
Constructor Summary
Constructors Constructor Description HashSet()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 Type Method Description booleanadd(E object)Adds the specified object to thisHashSetif not already present.voidclear()Removes all elements from thisHashSet, leaving it empty.Objectclone()Returns a newHashSetwith the same elements and size as thisHashSet.booleancontains(Object object)Searches thisHashSetfor the specified object.booleanisEmpty()Returns true if thisHashSethas no elements, false otherwise.Iterator<E>iterator()Returns an Iterator on the elements of thisHashSet.booleanremove(Object object)Removes the specified object from thisHashSet.intsize()Returns the number of elements in thisHashSet.Methods inherited from class java.util.AbstractSet
equals, hashCode, removeAllMethods inherited from class java.util.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 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. -
clone
Returns a newHashSetwith the same elements and size as thisHashSet. -
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:
size()
-
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.
-