public class HashSet<E>

  1. Object
  2. AbstractCollection<E>
  3. AbstractSet<E>
  4. HashSet

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

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

capacity int
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

capacity int
the initial capacity.
loadFactor float
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

collection Collection<? 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

object E
the object to add.

Returns

true when this HashSet did not already contain the object, false otherwise

clear

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

object Object
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

object Object
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.