Class HashSetLinear


java.lang.Object

  |

  +--java.util.AbstractCollection

        |

        +--java.util.AbstractSet

              |

              +--HashSetLinear


public class HashSetLinear
extends java.util.AbstractSet

This class implements a hash set with linear probing. It extends AbstractSet, with a implementations of add(), remove(), size() and iterator(). The iterator supports hasNext(), next(), and remove().

The element null cannot be stored in a HashSetLinear.

The iterators returned by this version of HashSetLinear are "failsafe": this is implemented by maintaining a modification counter. The Math 176 homework solutions do not need to support this feature.

One novel feature of this implementation is that it supports active deletion (which is not generally used with linear probing).

The objects in the set must support equals() and hashCode(). Two objects which test as being equal should have the same hash code. Otherwise, unpredictable results may occur.


Constructor Summary
HashSetLinear()
          This constuctor creates a new, empty HashSetLinear with an initial capacity of 101 and a load factor of 0.50.
HashSetLinear(int initialCapacity)
          This constuctor creates a new, empty HashSetLinear with a specified initial capacity and the default load factor of 0.50.
HashSetLinear(int initialCapacity, float loadFactor)
          This constuctor creates a new, empty HashSetLinear with a specified initial capacity and a specified load factor.
 
Method Summary
 boolean add(java.lang.Object o)
          Add an object to the set stored in the HashTableLinear.
 void clear()
          Removes all the elements from the set (not necessary for Math 176 homework).
 boolean contains(java.lang.Object o)
          Check whether an object is present in the hash table
 java.util.Iterator iterator()
          Create an iterator for the HashTableLinear set
 boolean remove(java.lang.Object o)
          Remove an object from the set stored in the HashTableLinear.
 int size()
          Return the number of elements in the hash table
 
Methods inherited from class java.util.AbstractSet
equals, hashCode
 
Methods inherited from class java.util.AbstractCollection
addAll, containsAll, isEmpty, removeAll, retainAll, toArray, toArray, toString
 
Methods inherited from class java.lang.Object
getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

HashSetLinear


public HashSetLinear()
This constuctor creates a new, empty HashSetLinear with an initial capacity of 101 and a load factor of 0.50.

HashSetLinear


public HashSetLinear(int initialCapacity)
This constuctor creates a new, empty HashSetLinear with a specified initial capacity and the default load factor of 0.50.
Parameters:
initialCapacity - inital size of the hash table
Throws:
java.lang.IllegalArgumentException - if the initial capacity is negative.

HashSetLinear


public HashSetLinear(int initialCapacity,
                     float loadFactor)
This constuctor creates a new, empty HashSetLinear with a specified initial capacity and a specified load factor. The hash set will be resized whenever the number of elements exceeds the size of the hashtable times the load factor. The present implementation never resizes downwards.
Parameters:
initialCapacity - inital size of the hash table
loadFactor - the load factor: not recommended to be higher than 0.75.
Throws:
java.lang.IllegalArgumentException - if the initial capacity is negative or if the loadFactor is not greater than zero and less than one.
Method Detail

iterator


public java.util.Iterator iterator()
Create an iterator for the HashTableLinear set
Overrides:
iterator in class java.util.AbstractCollection

size


public int size()
Return the number of elements in the hash table
Overrides:
size in class java.util.AbstractCollection

contains


public boolean contains(java.lang.Object o)
Check whether an object is present in the hash table
Overrides:
contains in class java.util.AbstractCollection
Parameters:
o - The Object whose presence is being checked.
Returns:
true if o is in the set.

add


public boolean add(java.lang.Object o)
Add an object to the set stored in the HashTableLinear. An object that is already present in the set will not be re-added. Thus the set never contains duplicate copies of the same object. The hash table will be resized if the addition of one more element would exceed the resize threshold.
Overrides:
add in class java.util.AbstractCollection
Parameters:
o - the Object to be added.
Returns:
true if the object was added, i.e., if the object was not already present in the set.

remove


public boolean remove(java.lang.Object o)
Remove an object from the set stored in the HashTableLinear.
Overrides:
remove in class java.util.AbstractCollection
Parameters:
o - the Object to be removed
Returns:
true if the object was removed, i.e., returns true if the object was present in the set to be removed.

clear


public void clear()
Removes all the elements from the set (not necessary for Math 176 homework).
Overrides:
clear in class java.util.AbstractCollection