Java in a Nutshell, 5th Edition [Electronic resources]

نسخه متنی -صفحه : 1191/ 714
نمايش فراداده

HashSet<E>java.util

Java 1.2cloneable serializable collection

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 ); public

HashSet (int

initialCapacity , float

loadFactor ); // Methods Implementing Set public boolean

add (E

o ); public void

clear ( ); public boolean

contains (Object

o ); public boolean

isEmpty ( ); default:true public Iterator<E>

iterator ( ); public boolean

remove (Object

o ); public int

size ( ); // Public Methods Overriding Object public Object

clone ( ); }

Subclasses

LinkedHashSet