Skip to content

Most visited

Recently visited

navigation
Added in API level 1

HashSet

public class HashSet
extends AbstractSet<E> implements Set<E>, Cloneable, Serializable

java.lang.Object
   ↳ java.util.AbstractCollection<E>
     ↳ java.util.AbstractSet<E>
       ↳ java.util.HashSet<E>
Known Direct Subclasses


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.

Inherited methods

From class java.util.AbstractSet
From class java.util.AbstractCollection
From class java.lang.Object
From interface java.util.Set
From interface java.util.Collection
From interface java.lang.Iterable

Public constructors

HashSet

Added in API level 1
HashSet ()

Constructs a new empty instance of HashSet.

HashSet

Added in API level 1
HashSet (int capacity)

Constructs a new instance of HashSet with the specified capacity.

Parameters
capacity int: the initial capacity of this HashSet.

HashSet

Added in API level 1
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

Added in API level 1
HashSet (Collection<? extends E> collection)

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

Parameters
collection Collection: the collection of elements to add.

Public methods

add

Added in API level 1
boolean add (E object)

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

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

clear

Added in API level 1
void clear ()

Removes all elements from this HashSet, leaving it empty.

See also:

clone

Added in API level 1
Object clone ()

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

Returns
Object a shallow copy of this HashSet.

See also:

contains

Added in API level 1
boolean contains (Object object)

Searches this HashSet for the specified object.

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

isEmpty

Added in API level 1
boolean isEmpty ()

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

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

See also:

iterator

Added in API level 1
Iterator<E> iterator ()

Returns an Iterator on the elements of this HashSet.

Returns
Iterator<E> an Iterator on the elements of this HashSet.

See also:

remove

Added in API level 1
boolean remove (Object object)

Removes the specified object from this HashSet.

Parameters
object Object: the object to remove.
Returns
boolean true if the object was removed, false otherwise.

size

Added in API level 1
int size ()

Returns the number of elements in this HashSet.

Returns
int the number of elements in this HashSet.
This site uses cookies to store your preferences for site-specific language and display options.

Hooray!

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.