java.util
Class HashSet<E>

java.lang.Object sample code for java.lang.Object definition code for java.lang.Object 
  extended by java.util.AbstractCollection sample code for java.util.AbstractCollection definition code for java.util.AbstractCollection <E>
      extended by java.util.AbstractSet sample code for java.util.AbstractSet definition code for java.util.AbstractSet <E>
          extended by java.util.HashSet<E>
All Implemented Interfaces:
Serializable sample code for java.io.Serializable definition code for java.io.Serializable , Cloneable sample code for java.lang.Cloneable definition code for java.lang.Cloneable , Iterable sample code for java.lang.Iterable definition code for java.lang.Iterable <E>, Collection sample code for java.util.Collection definition code for java.util.Collection <E>, Set sample code for java.util.Set definition code for java.util.Set <E>
Direct Known Subclasses:
JobStateReasons sample code for javax.print.attribute.standard.JobStateReasons definition code for javax.print.attribute.standard.JobStateReasons , LinkedHashSet sample code for java.util.LinkedHashSet definition code for java.util.LinkedHashSet

public class HashSet<E>
extends AbstractSet sample code for java.util.AbstractSet definition code for java.util.AbstractSet <E>
implements Set sample code for java.util.Set definition code for java.util.Set <E>, Cloneable sample code for java.lang.Cloneable definition code for java.lang.Cloneable , Serializable sample code for java.io.Serializable definition code for java.io.Serializable

This class implements the Set interface, backed by a hash table (actually a HashMap instance). It makes no guarantees as to the iteration order of the set; in particular, it does not guarantee that the order will remain constant over time. This class permits the null element.

This class offers constant time performance for the basic operations (add, remove, contains and size), assuming the hash function disperses the elements properly among the buckets. Iterating over this set requires time proportional to the sum of the HashSet instance's size (the number of elements) plus the "capacity" of the backing HashMap instance (the number of buckets). Thus, it's very important not to set the initial capacity too high (or the load factor too low) if iteration performance is important.

Note that this implementation is not synchronized. If multiple threads access a set concurrently, and at least one of the threads modifies the set, it must be synchronized externally. This is typically accomplished by synchronizing on some object that naturally encapsulates the set. If no such object exists, the set should be "wrapped" using the Collections.synchronizedSet method. This is best done at creation time, to prevent accidental unsynchronized access to the HashSet instance:

     Set s = Collections.synchronizedSet(new HashSet(...));
 

The iterators returned by this class's iterator method are fail-fast: if the set is modified at any time after the iterator is created, in any way except through the iterator's own remove method, the Iterator throws a ConcurrentModificationException. Thus, in the face of concurrent modification, the iterator fails quickly and cleanly, rather than risking arbitrary, non-deterministic behavior at an undetermined time in the future.

Note that the fail-fast behavior of an iterator cannot be guaranteed as it is, generally speaking, impossible to make any hard guarantees in the presence of unsynchronized concurrent modification. Fail-fast iterators throw ConcurrentModificationException on a best-effort basis. Therefore, it would be wrong to write a program that depended on this exception for its correctness: the fail-fast behavior of iterators should be used only to detect bugs.

This class is a member of the Java Collections Framework.

Since:
1.2
See Also:
Collection sample code for java.util.Collection definition code for java.util.Collection , Set sample code for java.util.Set definition code for java.util.Set , TreeSet sample code for java.util.TreeSet definition code for java.util.TreeSet , Collections.synchronizedSet(Set) sample code for java.util.Collections.synchronizedSet(java.util.Set) definition code for java.util.Collections.synchronizedSet(java.util.Set) , HashMap sample code for java.util.HashMap definition code for java.util.HashMap , Serialized Form

Constructor Summary
HashSet sample code for java.util.HashSet.HashSet() definition code for java.util.HashSet.HashSet() ()
          Constructs a new, empty set; the backing HashMap instance has default initial capacity (16) and load factor (0.75).
HashSet sample code for java.util.HashSet.HashSet(java.util.Collection) definition code for java.util.HashSet.HashSet(java.util.Collection) (Collection sample code for java.util.Collection definition code for java.util.Collection <? extends E> c)
          Constructs a new set containing the elements in the specified collection.
HashSet sample code for java.util.HashSet.HashSet(int) definition code for java.util.HashSet.HashSet(int) (int initialCapacity)
          Constructs a new, empty set; the backing HashMap instance has the specified initial capacity and default load factor, which is 0.75.
HashSet sample code for java.util.HashSet.HashSet(int, float) definition code for java.util.HashSet.HashSet(int, float) (int initialCapacity, float loadFactor)
          Constructs a new, empty set; the backing HashMap instance has the specified initial capacity and the specified load factor.
 
Method Summary
 boolean add sample code for java.util.HashSet.add(E) definition code for java.util.HashSet.add(E) (E o)
          Adds the specified element to this set if it is not already present.
 void clear sample code for java.util.HashSet.clear() definition code for java.util.HashSet.clear() ()
          Removes all of the elements from this set.
 Object sample code for java.lang.Object definition code for java.lang.Object clone sample code for java.util.HashSet.clone() definition code for java.util.HashSet.clone() ()
          Returns a shallow copy of this HashSet instance: the elements themselves are not cloned.
 boolean contains sample code for java.util.HashSet.contains(java.lang.Object) definition code for java.util.HashSet.contains(java.lang.Object) (Object sample code for java.lang.Object definition code for java.lang.Object  o)
          Returns true if this set contains the specified element.
 boolean isEmpty sample code for java.util.HashSet.isEmpty() definition code for java.util.HashSet.isEmpty() ()
          Returns true if this set contains no elements.
 Iterator sample code for java.util.Iterator definition code for java.util.Iterator <E> iterator sample code for java.util.HashSet.iterator() definition code for java.util.HashSet.iterator() ()
          Returns an iterator over the elements in this set.
 boolean remove sample code for java.util.HashSet.remove(java.lang.Object) definition code for java.util.HashSet.remove(java.lang.Object) (Object sample code for java.lang.Object definition code for java.lang.Object  o)
          Removes the specified element from this set if it is present.
 int size sample code for java.util.HashSet.size() definition code for java.util.HashSet.size() ()
          Returns the number of elements in this set (its cardinality).
 
Methods inherited from class java.util.AbstractSet sample code for java.util.AbstractSet definition code for java.util.AbstractSet
equals sample code for java.util.AbstractSet.equals(java.lang.Object) definition code for java.util.AbstractSet.equals(java.lang.Object) , hashCode sample code for java.util.AbstractSet.hashCode() definition code for java.util.AbstractSet.hashCode() , removeAll sample code for java.util.AbstractSet.removeAll(java.util.Collection) definition code for java.util.AbstractSet.removeAll(java.util.Collection)
 
Methods inherited from class java.util.AbstractCollection sample code for java.util.AbstractCollection definition code for java.util.AbstractCollection
addAll sample code for java.util.AbstractCollection.addAll(java.util.Collection) definition code for java.util.AbstractCollection.addAll(java.util.Collection) , containsAll sample code for java.util.AbstractCollection.containsAll(java.util.Collection) definition code for java.util.AbstractCollection.containsAll(java.util.Collection) , retainAll sample code for java.util.AbstractCollection.retainAll(java.util.Collection) definition code for java.util.AbstractCollection.retainAll(java.util.Collection) , toArray sample code for java.util.AbstractCollection.toArray() definition code for java.util.AbstractCollection.toArray() , toArray sample code for java.util.AbstractCollection.toArray(T[]) definition code for java.util.AbstractCollection.toArray(T[]) , toString sample code for java.util.AbstractCollection.toString() definition code for java.util.AbstractCollection.toString()
 
Methods inherited from class java.lang.Object sample code for java.lang.Object definition code for java.lang.Object
finalize sample code for java.lang.Object.finalize() definition code for java.lang.Object.finalize() , getClass sample code for java.lang.Object.getClass() definition code for java.lang.Object.getClass() , notify sample code for java.lang.Object.notify() definition code for java.lang.Object.notify() , notifyAll sample code for java.lang.Object.notifyAll() definition code for java.lang.Object.notifyAll() , wait sample code for java.lang.Object.wait() definition code for java.lang.Object.wait() , wait sample code for java.lang.Object.wait(long) definition code for java.lang.Object.wait(long) , wait sample code for java.lang.Object.wait(long, int) definition code for java.lang.Object.wait(long, int)
 
Methods inherited from interface java.util.Set sample code for java.util.Set definition code for java.util.Set
addAll sample code for java.util.Set.addAll(java.util.Collection) definition code for java.util.Set.addAll(java.util.Collection) , containsAll sample code for java.util.Set.containsAll(java.util.Collection) definition code for java.util.Set.containsAll(java.util.Collection) , equals sample code for java.util.Set.equals(java.lang.Object) definition code for java.util.Set.equals(java.lang.Object) , hashCode sample code for java.util.Set.hashCode() definition code for java.util.Set.hashCode() , removeAll sample code for java.util.Set.removeAll(java.util.Collection) definition code for java.util.Set.removeAll(java.util.Collection) , retainAll sample code for java.util.Set.retainAll(java.util.Collection) definition code for java.util.Set.retainAll(java.util.Collection) , toArray sample code for java.util.Set.toArray() definition code for java.util.Set.toArray() , toArray sample code for java.util.Set.toArray(T[]) definition code for java.util.Set.toArray(T[])
 

Constructor Detail

HashSet sample code for java.util.HashSet() definition code for java.util.HashSet()

public HashSet()
Constructs a new, empty set; the backing HashMap instance has default initial capacity (16) and load factor (0.75).


HashSet sample code for java.util.HashSet(java.util.Collection<? extends E>) definition code for java.util.HashSet(java.util.Collection<? extends E>)

public HashSet(Collection sample code for java.util.Collection definition code for java.util.Collection <? extends E> c)
Constructs a new set containing the elements in the specified collection. The HashMap is created with default load factor (0.75) and an initial capacity sufficient to contain the elements in the specified collection.

Parameters:
c - the collection whose elements are to be placed into this set.
Throws:
NullPointerException sample code for java.lang.NullPointerException definition code for java.lang.NullPointerException - if the specified collection is null.

HashSet sample code for java.util.HashSet(int, float) definition code for java.util.HashSet(int, float)

public HashSet(int initialCapacity,
               float loadFactor)
Constructs a new, empty set; the backing HashMap instance has the specified initial capacity and the specified load factor.

Parameters:
initialCapacity - the initial capacity of the hash map.
loadFactor - the load factor of the hash map.
Throws:
IllegalArgumentException sample code for java.lang.IllegalArgumentException definition code for java.lang.IllegalArgumentException - if the initial capacity is less than zero, or if the load factor is nonpositive.

HashSet sample code for java.util.HashSet(int) definition code for java.util.HashSet(int)

public HashSet(int initialCapacity)
Constructs a new, empty set; the backing HashMap instance has the specified initial capacity and default load factor, which is 0.75.

Parameters:
initialCapacity - the initial capacity of the hash table.
Throws:
IllegalArgumentException sample code for java.lang.IllegalArgumentException definition code for java.lang.IllegalArgumentException - if the initial capacity is less than zero.
Method Detail

iterator sample code for java.util.HashSet.iterator() definition code for java.util.HashSet.iterator()

public Iterator sample code for java.util.Iterator definition code for java.util.Iterator <E> iterator()
Returns an iterator over the elements in this set. The elements are returned in no particular order.

Specified by:
iterator sample code for java.lang.Iterable.iterator() definition code for java.lang.Iterable.iterator() in interface Iterable sample code for java.lang.Iterable definition code for java.lang.Iterable <E>
Specified by:
iterator sample code for java.util.Collection.iterator() definition code for java.util.Collection.iterator() in interface Collection sample code for java.util.Collection definition code for java.util.Collection <E>
Specified by:
iterator sample code for java.util.Set.iterator() definition code for java.util.Set.iterator() in interface Set sample code for java.util.Set definition code for java.util.Set <E>
Specified by:
iterator sample code for java.util.AbstractCollection.iterator() definition code for java.util.AbstractCollection.iterator() in class AbstractCollection sample code for java.util.AbstractCollection definition code for java.util.AbstractCollection <E>
Returns:
an Iterator over the elements in this set.
See Also:
ConcurrentModificationException sample code for java.util.ConcurrentModificationException definition code for java.util.ConcurrentModificationException

size sample code for java.util.HashSet.size() definition code for java.util.HashSet.size()

public int size()
Returns the number of elements in this set (its cardinality).

Specified by:
size sample code for java.util.Collection.size() definition code for java.util.Collection.size() in interface Collection sample code for java.util.Collection definition code for java.util.Collection <E>
Specified by:
size sample code for java.util.Set.size() definition code for java.util.Set.size() in interface Set sample code for java.util.Set definition code for java.util.Set <E>
Specified by:
size sample code for java.util.AbstractCollection.size() definition code for java.util.AbstractCollection.size() in class AbstractCollection sample code for java.util.AbstractCollection definition code for java.util.AbstractCollection <E>
Returns:
the number of elements in this set (its cardinality).

isEmpty sample code for java.util.HashSet.isEmpty() definition code for java.util.HashSet.isEmpty()

public boolean isEmpty()
Returns true if this set contains no elements.

Specified by:
isEmpty sample code for java.util.Collection.isEmpty() definition code for java.util.Collection.isEmpty() in interface Collection sample code for java.util.Collection definition code for java.util.Collection <E>
Specified by:
isEmpty sample code for java.util.Set.isEmpty() definition code for java.util.Set.isEmpty() in interface Set sample code for java.util.Set definition code for java.util.Set <E>
Overrides:
isEmpty sample code for java.util.AbstractCollection.isEmpty() definition code for java.util.AbstractCollection.isEmpty() in class AbstractCollection sample code for java.util.AbstractCollection definition code for java.util.AbstractCollection <E>
Returns:
true if this set contains no elements.

contains sample code for java.util.HashSet.contains(java.lang.Object) definition code for java.util.HashSet.contains(java.lang.Object)

public boolean contains(Object sample code for java.lang.Object definition code for java.lang.Object  o)
Returns true if this set contains the specified element.

Specified by:
contains sample code for java.util.Collection.contains(java.lang.Object) definition code for java.util.Collection.contains(java.lang.Object) in interface Collection sample code for java.util.Collection definition code for java.util.Collection <E>
Specified by:
contains sample code for java.util.Set.contains(java.lang.Object) definition code for java.util.Set.contains(java.lang.Object) in interface Set sample code for java.util.Set definition code for java.util.Set <E>
Overrides:
contains sample code for java.util.AbstractCollection.contains(java.lang.Object) definition code for java.util.AbstractCollection.contains(java.lang.Object) in class AbstractCollection sample code for java.util.AbstractCollection definition code for java.util.AbstractCollection <E>
Parameters:
o - element whose presence in this set is to be tested.
Returns:
true if this set contains the specified element.

add sample code for java.util.HashSet.add(E) definition code for java.util.HashSet.add(E)

public boolean add(E o)
Adds the specified element to this set if it is not already present.

Specified by:
add sample code for java.util.Collection.add(E) definition code for java.util.Collection.add(E) in interface Collection sample code for java.util.Collection definition code for java.util.Collection <E>
Specified by:
add sample code for java.util.Set.add(E) definition code for java.util.Set.add(E) in interface Set sample code for java.util.Set definition code for java.util.Set <E>
Overrides:
add sample code for java.util.AbstractCollection.add(E) definition code for java.util.AbstractCollection.add(E) in class AbstractCollection sample code for java.util.AbstractCollection definition code for java.util.AbstractCollection <E>
Parameters:
o - element to be added to this set.
Returns:
true if the set did not already contain the specified element.

remove sample code for java.util.HashSet.remove(java.lang.Object) definition code for java.util.HashSet.remove(java.lang.Object)

public boolean remove(Object sample code for java.lang.Object definition code for java.lang.Object  o)
Removes the specified element from this set if it is present.

Specified by:
remove sample code for java.util.Collection.remove(java.lang.Object) definition code for java.util.Collection.remove(java.lang.Object) in interface Collection sample code for java.util.Collection definition code for java.util.Collection <E>
Specified by:
remove sample code for java.util.Set.remove(java.lang.Object) definition code for java.util.Set.remove(java.lang.Object) in interface Set sample code for java.util.Set definition code for java.util.Set <E>
Overrides:
remove sample code for java.util.AbstractCollection.remove(java.lang.Object) definition code for java.util.AbstractCollection.remove(java.lang.Object) in class AbstractCollection sample code for java.util.AbstractCollection definition code for java.util.AbstractCollection <E>
Parameters:
o - object to be removed from this set, if present.
Returns:
true if the set contained the specified element.

clear sample code for java.util.HashSet.clear() definition code for java.util.HashSet.clear()

public void clear()
Removes all of the elements from this set.

Specified by:
clear sample code for java.util.Collection.clear() definition code for java.util.Collection.clear() in interface Collection sample code for java.util.Collection definition code for java.util.Collection <E>
Specified by:
clear sample code for java.util.Set.clear() definition code for java.util.Set.clear() in interface Set sample code for java.util.Set definition code for java.util.Set <E>
Overrides:
clear sample code for java.util.AbstractCollection.clear() definition code for java.util.AbstractCollection.clear() in class AbstractCollection sample code for java.util.AbstractCollection definition code for java.util.AbstractCollection <E>

clone sample code for java.util.HashSet.clone() definition code for java.util.HashSet.clone()

public Object sample code for java.lang.Object definition code for java.lang.Object  clone()
Returns a shallow copy of this HashSet instance: the elements themselves are not cloned.

Overrides:
clone sample code for java.lang.Object.clone() definition code for java.lang.Object.clone() in class Object sample code for java.lang.Object definition code for java.lang.Object
Returns:
a shallow copy of this set.
See Also:
Cloneable sample code for java.lang.Cloneable definition code for java.lang.Cloneable