/* * HashTableExample.java * */ import net.datastructures.EqualityTester; import java.util.Random; import java.util.Date; public class HashTableExample { static final int CAPACITY = 10001; static final int DEFAULT_ENTRY_NUM = 3000; public static void main(String[] args) { int i; int n; /**** select the number of entres ****/ if (args.length == 0) n = DEFAULT_ENTRY_NUM; else n = Integer.parseInt(args[0]); /*** create a hassh table ***********/ EqualityTester tester = new HashTable.DefaultEqualityTester(); HashTable table = new HashTable(CAPACITY, tester); int[] a = new int[n]; /* set the initial value randomly for the array a */ Random r = new Random(new Date().getTime()); for (i=0; i< a.length; i++) { a[i] = r.nextInt(100000); } /* insert the element in a into hashtable and get the total number of probes */ long totalProbe = 0; for (i=0; i < a.length; i++) { totalProbe += table.getProbeNum(a[i]); table.put(a[i], a[i]); } System.out.println ("The average probe number is: " + (float)totalProbe/n); int x =0; for (i=0; i < 50000; i++) { Integer result = (Integer)table.get(i); if (result != null) x=x+1; } System.out.println("The number of entry is " + x); } }