Bill Teter
Office:          149 Redcay
Telephone:    2782
Office Hours:  Tuesday, Thursday 9:00-12:00

email:  william.teter@plattsburgh.edu

       

 

Project 7  Hash functions

The purpose of this project is to compare two hash functions for social security numbers.  One of the hash functions you will use is the hashcode() function for strings that is part of the String class in java.  The other hash function you will create.  Its should be a universal hash function defined for a table size of 20,000.  Your task is to count how many times the equals() function is called when inserting the same 10,000 randomly generated social security numbers using each of the above mentioned two hashing functions into two HashSets.  The number of times equals() is called is the number of collisions that were created in the process of adding the 10,000 social security number to each of the sets.

1.  You will need two Social Security Number classes.  Each should have a constructor that creates an object from a String parameter, a hashcode function that overrides the hashcode() method in Object, a method called equals() that overrides the equals() method in Object that additionally increments a static counter field in the Social Security Number class whenever equals() is invoked.  This should count a collision when inserting into the hash sets.

2.  A test program will create two hashsets with initial capacity of 20,000.  Randomly generate 10,000 social security numbers and insert each number into both hash sets.  Then print the total number of collisions after all insertions for each hash set.  You should use the HashSet class in weiss.util.HashSet. and the interface weiss.util.Set.  Change the default capacity constant to 20000.  The java.util.HashSet class does not call the equals() method every time a collision occurs.  I'm not sure why.

Advice:
    When you override hashCode and equals make sure the signatures of hashCode() and equals() match precisely what they are in the Object class.
    The java API requires that if x.equals(y) then x.hashCode() == y.hashCode().