to top
Android APIs
public class

HashSet

extends AbstractSet<E>
implements Serializable Cloneable Set<E>
java.lang.Object
   ↳ java.util.AbstractCollection<E>
     ↳ java.util.AbstractSet<E>
       ↳ java.util.HashSet<E>
Known Direct Subclasses

Class Overview

HashSet is an implementation of a Set. All optional operations (adding and removing) are supported. The elements can be any objects.

Summary

Public Constructors
HashSet()
Constructs a new empty instance of HashSet.
HashSet(int capacity)
Constructs a new instance of HashSet with the specified capacity.
HashSet(int capacity, float loadFactor)
Constructs a new instance of HashSet with the specified capacity and load factor.
HashSet(Collection<? extends E> collection)
Constructs a new instance of HashSet containing the unique elements in the specified collection.
Public Methods
boolean add(E object)
Adds the specified object to this HashSet if not already present.
void clear()
Removes all elements from this HashSet, leaving it empty.
Object clone()
Returns a new HashSet with the same elements and size as this HashSet.
boolean contains(Object object)
Searches this HashSet for the specified object.
boolean isEmpty()
Returns true if this HashSet has no elements, false otherwise.
Iterator<E> iterator()
Returns an Iterator on the elements of this HashSet.
boolean remove(Object object)
Removes the specified object from this HashSet.
int size()
Returns the number of elements in this HashSet.
[Expand]
Inherited Methods
From class java.util.AbstractSet
From class java.util.AbstractCollection
From class java.lang.Object
From interface java.lang.Iterable
From interface java.util.Collection
From interface java.util.Set

Public Constructors

public HashSet ()

Since: API Level 1

Constructs a new empty instance of HashSet.

public HashSet (int capacity)

Since: API Level 1

Constructs a new instance of HashSet with the specified capacity.

Parameters
capacity the initial capacity of this HashSet.

public HashSet (int capacity, float loadFactor)

Since: API Level 1

Constructs a new instance of HashSet with the specified capacity and load factor.

Parameters
capacity the initial capacity.
loadFactor the initial load factor.

public HashSet (Collection<? extends E> collection)

Since: API Level 1

Constructs a new instance of HashSet containing the unique elements in the specified collection.

Parameters
collection the collection of elements to add.

Public Methods

public boolean add (E object)

Since: API Level 1

Adds the specified object to this HashSet if not already present.

Parameters
object the object to add.
Returns
  • true when this HashSet did not already contain the object, false otherwise

public void clear ()

Since: API Level 1

Removes all elements from this HashSet, leaving it empty.

See Also

public Object clone ()

Since: API Level 1

Returns a new HashSet with the same elements and size as this HashSet.

Returns
  • a shallow copy of this HashSet.
See Also

public boolean contains (Object object)

Since: API Level 1

Searches this HashSet for the specified object.

Parameters
object the object to search for.
Returns
  • true if object is an element of this HashSet, false otherwise.

public boolean isEmpty ()

Since: API Level 1

Returns true if this HashSet has no elements, false otherwise.

Returns
  • true if this HashSet has no elements, false otherwise.
See Also

public Iterator<E> iterator ()

Since: API Level 1

Returns an Iterator on the elements of this HashSet.

Returns
  • an Iterator on the elements of this HashSet.
See Also

public boolean remove (Object object)

Since: API Level 1

Removes the specified object from this HashSet.

Parameters
object the object to remove.
Returns
  • true if the object was removed, false otherwise.

public int size ()

Since: API Level 1

Returns the number of elements in this HashSet.

Returns
  • the number of elements in this HashSet.