Name
HashSet<E>
Synopsis
This class implements
Set
using an internal hashtable. It supports all
optional Set
and Collection
methods and allows any type of object or null
to
be a member of the set. Because HashSet
is based
on a hashtable, the basic add( )
, remove(
)
, and contains( )
methods are all quite
efficient. HashSet
makes no guarantee about the
order in which the set elements are enumerated by the
Iterator
returned by iterator(
)
. The methods of HashSet
are not
synchronized
. If you are using it in a
multithreaded environment, you must explicitly synchronize all code
that modifies the set or obtain a synchronized wrapper for it by
calling Collections.synchronizedSet( )
.
If you know in advance approximately how many mappings a
HashSet
will contain, you can improve efficiency
by specifying initialCapacity
when you
call the HashSet( )
constructor. The
initialCapacity
argument times the
loadFactor
argument should be greater than
the number of mappings the HashSet
will contain. A
good value for loadFactor
is 0.75; this is
also the default value. See Set
and
Collection
for details on the methods of
HashSet
. See also TreeSet
and
HashMap
.
Figure 16-25. java.util.HashSet<E>
public class HashSet<E> extends AbstractSet<E> implements Set<E>, Cloneable, Serializable { // Public Constructors public HashSet( ); public HashSet(Collection<? extends E> c); public HashSet(int initialCapacity); ...
Get Java in a Nutshell, 5th Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.