CS 46B - Lecture 20

Cover page image

Pre-class reading

Implementing a Hash Table

Hash Functions

Example: Hash code for String

final int HASH_MULTIPLIER = 31;
int h = 0;
for (int i = 0; i < s.length(); i++)
   h = HASH_MULTIPLIER * h + s.charAt(i); 

Lecture 20 Clicker Question 1

Trivia fact of the day: 'd' has code 100.

What is the hash code for the string "cab"?

  1. 294
  2. 98244
  3. 97284
  4. Something else

Implementing Hash Functions

public class Country
{
   public int hashCode()
   {
      int h1 = name.hashCode();
      int h2 = new Double(area).hashCode();
      final int HASH_MULTIPLIER = 31;
      int h = HASH_MULTIPLIER * h1 + h2;
      return h;
   }
}

Collisions

Finding an Element

Adding an Element

Lecture 20 Clicker Question 2

Fred makes a hash table of Employee objects and defines an equals method

public boolean equals(Object other) { return id == ((Employee) other).id; }

But he forgets to implement a hashCode method for the Employee class What happens?

  1. He gets a compile-time error
  2. He gets a run-time exception
  3. When adding multiple employee objects with the same ID, he sometimes ends up with more than one
  4. When adding multiple employee objects with the same ID, he always ends up with more than one

Lecture 20 Clicker Question 3

Fred makes a hash table of Employee objects and defines a hashCode method

public int hashCode() { return id; }

But he forgets to implement an equals method for the Employee class What happens?

  1. He gets a compile-time error
  2. He gets a run-time exception
  3. When adding multiple employee objects with the same ID, he sometimes ends up with more than one
  4. When adding multiple employee objects with the same ID, he always ends up with more than one

Lecture 20 Clicker Question 4

Fred makes a hash table of Employee objects and defines a hashCode method

public int hashCode() { return id; }

and an equals method

public boolean equals(Employee other) { return id == other.id; }

But his hash table doesn't work right. What happens?

  1. He gets a compile-time error
  2. He gets a run-time exception
  3. When adding multiple employee objects with the same ID, he sometimes ends up with more than one
  4. When adding multiple employee objects with the same ID, he always ends up with more than one

Lecture 20 Clicker Question 5

Fred makes a hash table of Employee objects and defines an equals method

public boolean equals(Object other) { return id == ((Employee) other).id; }

and a hashCode method

public int hashCode() { return -1; }

What happens?

  1. He gets a compile-time error
  2. He gets a run-time exception
  3. When adding multiple employee objects with the same ID, he sometimes ends up with more than one
  4. The hash table works correctly, but its performance is disappointing

Iterating over a Hash Table

Lecture 20 Clicker Question 6

Suppose you have two hash tables, each with n elements. To find the elements that are in both tables, you iterate over the first table, and for each element, check whether it is contained in the second table. What is the big-Oh efficiency of this algorithm?

  1. O(1)
  2. O(1)+
  3. O(n)
  4. O(n2)