public class HashSet<E>
ImplementsCollection<E>, Iterable<E>, Set<E>
Known subtypesLinkedHashSet
HashSet is an implementation of a Set. All optional operations (adding and
removing) are supported. The elements can be any objects.
Constructors
public HashSet() | Constructs a new empty instance of HashSet. |
public HashSet(int capacity) | Constructs a new instance of HashSet with the specified capacity. |
public HashSet(int capacity, float loadFactor) | Constructs a new instance of HashSet with the specified capacity and load factor. |
public HashSet(Collection<? extends E> collection) | Constructs a new instance of HashSet containing the unique elements in the specified collection. |
Methods
public boolean add(E object) | Adds the specified object to this HashSet if not already present. |
public void clear() | Removes all elements from this HashSet, leaving it empty. |
public boolean contains(Object object) | Searches this HashSet for the specified object. |
public boolean isEmpty() | Returns true if this HashSet has no elements, false otherwise. |
public Iterator<E> iterator() | Returns an Iterator on the elements of this HashSet. |
public boolean remove(Object object) | Removes the specified object from this HashSet. |
public int size() | Returns the number of elements in this HashSet. |
Inherited methods
From AbstractSet
From Collection
Constructor details
HashSet
public HashSet()Constructs a new empty instance of
HashSet.HashSet
public HashSet(int capacity)Constructs a new instance of
HashSet with the specified capacity.Parameters
capacityint- the initial capacity of this
HashSet.
HashSet
public HashSet(int capacity, float loadFactor)Constructs a new instance of
HashSet with the specified capacity
and load factor.Parameters
capacityint- the initial capacity.
loadFactorfloat- the initial load factor.
HashSet
public HashSet(Collection<? extends E> collection)Constructs a new instance of
HashSet containing the unique
elements in the specified collection.Parameters
collectionCollection<? extends E>- the collection of elements to add.
Method details
add
public boolean add(E object)Adds the specified object to this
HashSet if not already present.Parameters
objectE- the object to add.
Returns
true when this HashSet did not already contain
the object, false otherwiseclear
public void clear()Removes all elements from this
HashSet, leaving it empty.Throws
UnsupportedOperationException- it the iterator does not support removing elements from
this
Collection
contains
public boolean contains(Object object)Searches this
HashSet for the specified object.Parameters
objectObject- the object to search for.
Returns
true if object is an element of this
HashSet, false otherwise.isEmpty
public boolean isEmpty()Returns true if this
HashSet has no elements, false otherwise.Returns
true if this HashSet has no elements,
false otherwise.See also
iterator
public Iterator<E> iterator()Returns an Iterator on the elements of this
HashSet.Returns
an Iterator on the elements of this
HashSet.See also
remove
public boolean remove(Object object)Removes the specified object from this
HashSet.Parameters
objectObject- the object to remove.
Returns
true if the object was removed, false otherwise.size
public int size()Returns the number of elements in this
HashSet.Returns
the number of elements in this
HashSet.