Lecture 9: Hash Tables continued. 2/25

Comment on closed hashing

The hash tables discussed in Dale, Joyce, and Weems (sec. 10.6) are somewhat different from ours. In their implementation, the hash table is an array of key-value pairs rather than an array of headers of linked lists. This is called Closed hashing. The technique for dealing with collisions becomes more complicated in closed hashing.

equals() and hashCode() for complex data structures:

Java provides an equals(X) method and a hashCode() method for an arbitrary object. However, the default is that both these methods are based on the address in memory of the address.

Example: TestEqualLists1.java

Sometimes this is what you want, but often it is not. For example, you would like two linked lists to be considered equal if they have the identical sequence of elements; and that this sense of ``equals'' should be used by all the functions, including library functions, that call the equals method. Likewise, you might like to use a list like [1, 5, 8] as a key, and then look it up with a different list [1, 5, 8], without it having to be the same actual object.

The solution is to override the equals(X) and the hashCode() methods: Example using lists of ints: TestEqualLists2.java

Your new equals(X) and hashCode() methods must satisfy the following constraints; otherwise the functions that call these (lots of things, in the case of equals(); hash tables in the case of hashCode()) will fail in strange and unpredictable ways.

The method equals(X) must be an equivalence relation. That is:

The method hashCode() must be compatible with equals(X). That is, if X.equals(Y) returns true, then X.hashCode() and Y.hashCode() must be equal.

If you do want equality in the sense of ``the identical object'', then you can always use X == Y.

For complex data structures or mathematical entities, the question of what it means for two things to be the same, and how you compute that can be a deep and difficult one.

Using Hashing for Large Sets of Large Objects Note: This will not be on either the problem sets or the exams.