public class HashTablesDemo
1: //HashTablesDemo.java (from zyDE 5.10.1)
3: public class HashTablesDemo
4: {
5: public static void main(String[] args)
6: {
7: String[] keys =
8: {
9: "Los Angeles", "Houston", "Washington",
10: "Chicago", "San Francisco", "Dallas",
11: "Tokyo", "New York", "Vancouver"
12: };
13: String[] values =
14: {
15: "LAX", "IAH", "IAD",
16: "ORD", "SFO", "DAL",
17: "NRT", "JFK", "YVR"
18: };
20: final int initialCapacity = 11;
22: // Initialize the four types of hash tables
23: HashTable[] tables =
24: {
25: new ChainingHashTable(initialCapacity),
26: new LinearProbingHashTable(initialCapacity),
27: new QuadraticProbingHashTable(1, 1, initialCapacity),
28: new DoubleHashingHashTable(initialCapacity)
29: };
31: // Add the same content to each hash table
32: for (int i = 0; i < keys.length; i++)
33: {
34: // Insert the item into each hash table
35: for (int j = 0; j < tables.length; j++)
36: {
37: tables[j].insert(keys[i], values[i]);
38: }
39: }
41: // Print each table's buckets
42: String[] tableNames =
43: {
44: "Chaining",
45: "Linear probing",
46: "Quadratic probing",
47: "Double hashing"
48: };
49: for (int j = 0; j < tables.length; j++)
50: {
51: System.out.printf("%s: initial table:\n", tableNames[j]);
52: System.out.println(tables[j]);
53: }
55: // Remove the 3 oldest keys from each hash table
56: for (int i = 0; i < 3; i++)
57: {
58: for (int j = 0; j < tables.length; j++)
59: {
60: tables[j].remove(keys[i]);
61: }
62: }
64: // Print each table's buckets again
65: System.out.println();
66: for (int j = 0; j < tables.length; j++)
67: {
68: System.out.printf("%s after removal:\n", tableNames[j]);
69: System.out.println(tables[j]);
70: }
71: }
72: }