java.util
Class HashSet

java.lang.Object
  extended by java.util.AbstractCollection
      extended by java.util.HashSet
All Implemented Interfaces:
Iterable, Collection, Set

public class HashSet
extends AbstractCollection
implements Set

An implementation of Set that offers constant time retrieval and amortized constant time insertion. This implementation requires that the objects in it have a valid implementation of hashCode. The implementation should return an integer in the range of tagged integers: (2^29) to 2^29 - 1. The implementation that is inherited from Object is not suitable! Strings and Integers have a suitable implementation. This HashSet is based on a HashMap.


Constructor Summary
HashSet()
           
HashSet(int initialCapacity)
           
 
Method Summary
 boolean add(Object o)
          If the specified element is not contained within this collection, and addition of this element succeeds, then true will be returned.
 void clear()
          Removes all the elements in this collection.
 boolean contains(Object o)
          Searches this Collection for the specified object.
 Iterator iterator()
          Answers an Iterator on the elements of this Collection.
 boolean remove(Object o)
          Removes the first occurrence of the specified object from this Collection.
 boolean removeAll(Collection other)
          Removes all occurrences in this Collection of each object in the specified Collection.
 int size()
          Answers the number of elements in this Collection.
 Object[] toArray()
          Answers a new array containing all elements contained in this Collection.
 
Methods inherited from class java.util.AbstractCollection
addAll, containsAll, isEmpty, retainAll, toArray, toString
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, println
 
Methods inherited from interface java.util.Set
containsAll
 
Methods inherited from interface java.util.Collection
addAll, equals, hashCode, isEmpty, retainAll, toArray
 

Constructor Detail

HashSet

public HashSet()

HashSet

public HashSet(int initialCapacity)
Method Detail

add

public boolean add(Object o)
Description copied from class: AbstractCollection
If the specified element is not contained within this collection, and addition of this element succeeds, then true will be returned. If the specified element is already contained within this collection, or duplication is not permitted, false will be returned. Different implementations may add specific limitations on this method to filter permitted elements. For example, in some implementation, null element may be denied, and NullPointerException will be thrown out. These limitations should be explicitly documented by specific collection implmentation. Add operation is not supported in this implementation, and UnsupportedOperationException will always be thrown out.

Specified by:
add in interface Collection
Specified by:
add in interface Set
Overrides:
add in class AbstractCollection
Parameters:
o - the element to be added.
Returns:
true if the collection is changed successfully after invoking this method. Otherwise, false.

remove

public boolean remove(Object o)
Description copied from class: AbstractCollection
Removes the first occurrence of the specified object from this Collection. This operation traverses over the collection, looking for the specified object. Once the object is found, the object will be removed from the collection using the iterator's remove method. This collection will throw an UnsupportedOperationException if the iterator returned does not implement remove method, and the specified object is in this collection.

Specified by:
remove in interface Collection
Specified by:
remove in interface Set
Overrides:
remove in class AbstractCollection
Parameters:
o - the object to remove
Returns:
true if this Collection is modified, false otherwise

removeAll

public boolean removeAll(Collection other)
Description copied from class: AbstractCollection
Removes all occurrences in this Collection of each object in the specified Collection. This operation traverses over the collection itself, to verify whether each element is contained in the specified collection. The object will be removed from the collection itself using the iterator's remove method if it is contained in the specified collection. This collection will throw an UnsupportedOperationException if the iterator returned does not implement remove method, and the element in the specified collection is contained in this collection.

Specified by:
removeAll in interface Collection
Overrides:
removeAll in class AbstractCollection
Parameters:
other - the Collection of objects to remove
Returns:
true if this Collection is modified, false otherwise

contains

public boolean contains(Object o)
Description copied from class: AbstractCollection
Searches this Collection for the specified object.

Specified by:
contains in interface Collection
Specified by:
contains in interface Set
Overrides:
contains in class AbstractCollection
Parameters:
o - the object to search for
Returns:
true if object is an element of this Collection, false otherwise

iterator

public Iterator iterator()
Description copied from class: AbstractCollection
Answers an Iterator on the elements of this Collection. A subclass must implement the abstract methods iterator() and size().

Specified by:
iterator in interface Iterable
Specified by:
iterator in interface Collection
Specified by:
iterator in interface Set
Specified by:
iterator in class AbstractCollection
Returns:
an Iterator on the elements of this Collection
See Also:
Iterator

toArray

public Object[] toArray()
Description copied from class: AbstractCollection
Answers a new array containing all elements contained in this Collection. All the elements in the array will not be referenced by the collection. The elements in the returned array will be sorted to the same order as those returned by the iterator of this collection itself if the collection guarantees the order.

Specified by:
toArray in interface Collection
Specified by:
toArray in interface Set
Overrides:
toArray in class AbstractCollection
Returns:
an array of the elements from this Collection

size

public int size()
Description copied from class: AbstractCollection
Answers the number of elements in this Collection.

Specified by:
size in interface Collection
Specified by:
size in class AbstractCollection
Returns:
the number of elements in this Collection

clear

public void clear()
Description copied from class: AbstractCollection
Removes all the elements in this collection. This collection will be cleared up after this operation. The operation iterates over the collection, removes every element using Iterator.remove method. UnsupportedOperationException will be thrown out if the iterator returned by this collection does not implement the remove method and the collection is not zero length.

Specified by:
clear in interface Collection
Specified by:
clear in interface Set
Overrides:
clear in class AbstractCollection
See Also:
Collection.isEmpty(), Collection.size()