APCS Java Subset

ap.java.util
Class HashSet

java.lang.Object
  extended byap.java.util.HashSet
All Implemented Interfaces:
Set

public class HashSet
extends java.lang.Object
implements Set

This collection implements the Set interface using a hash table so that add, remove, and contains each execute in constant or O(1) time assuming reasonable distribution of elements by their hashCode values. The HashSet class is only used in the AB course.

The order in which elements are returned by a HashSet's iterator is not specified. It is possible that the order will change during a program's execution if the internal hash table is restructured, e.g., if it gets too full. Iterating over elements requires time proportional to the number of elements stored in the HashSet plus the capacity or number of buckets used in the internal hash table.


In the Sun JDK 1.4 an array of singly-linked lists is used in the internal implementation of of the class.

Though not part of the AP Java subset, a HashSet object can be constructed by specifying the initial number of buckets (the capacity) and the load factor determining when all elements are re-hashed. See the regular JDK API for details.


Constructor Summary
HashSet()
          Construct an initially empty set with the default capacity and load factor.
 
Method Summary
 boolean add(java.lang.Object o)
          Adds the parameter as an element to this set if it is not already stored in this set.
 boolean contains(java.lang.Object o)
          Returns true if this set contains the element passed as an argument, otherwise returns false.
 Iterator iterator()
          Returns an Iterator that provides access to the the elements of this set.
 boolean remove(java.lang.Object o)
          Removes the argument from this set and returns true if the argument is in this set; if not present returns false.
 int size()
          Returns the number of elements in this set.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

HashSet

public HashSet()
Construct an initially empty set with the default capacity and load factor.

Method Detail

add

public boolean add(java.lang.Object o)
Description copied from interface: Set
Adds the parameter as an element to this set if it is not already stored in this set. If the element is added, the function returns true. If the element is not added because it is already stored in the set, the function returns false.

Specified by:
add in interface Set
Parameters:
o - is the element to be added to this set
Returns:
true if o is added, false if not added because it is already in the set

contains

public boolean contains(java.lang.Object o)
Description copied from interface: Set
Returns true if this set contains the element passed as an argument, otherwise returns false. More specifically, returns true if this set contains an element e such that e.equals(o).

Specified by:
contains in interface Set
Returns:
true if this set contains the argument, and false if the argument is not in this set.

remove

public boolean remove(java.lang.Object o)
Description copied from interface: Set
Removes the argument from this set and returns true if the argument is in this set; if not present returns false.

Specified by:
remove in interface Set
Parameters:
o - is the element to be removed from this set
Returns:
true if element is removed, otherwise return false

size

public int size()
Description copied from interface: Set
Returns the number of elements in this set.

Specified by:
size in interface Set
Returns:
the number of elements in the set

iterator

public Iterator iterator()
Returns an Iterator that provides access to the the elements of this set. Elements are returned in no particular order.

Specified by:
iterator in interface Set
Returns:
an iterator that provides access to elements from this set in ascending order

unofficial documentation for the APCS Java Subset