-
Notifications
You must be signed in to change notification settings - Fork 0
/
HashTesting.java
61 lines (45 loc) · 1.93 KB
/
HashTesting.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/********************************************************************************************
* File: HashTesting.java
* CS103 Data Structures - Project # 5 Analysis of Hashing versus Double Hashing
*
* Authors: Donald Craig and Joe Eckstein
* Date: 05/12/2018
*
********************************************************************************************/
import java.util.*;
import java.io.*;
class HashTesting {
public static void main(String [] args) throws IOException
{
int capacity = 241;
String firstName;
int keyNumber;
Table<Integer, String> hashTable = new Table<Integer, String>(capacity);
TableDoubleHash<Integer, String> doubleHashTable = new TableDoubleHash<Integer, String>(capacity);
File text = new File ("names.txt");
if (text.exists())
{
System.out.println("Input file exists. Reading...");
System.out.println();
}
else
{
text.createNewFile();
}
Scanner read = new Scanner(text);
while(read.hasNext())
{
firstName = read.next();
keyNumber = read.nextInt();
hashTable.put(keyNumber, firstName);
doubleHashTable.put(keyNumber, firstName);
}//end while
System.out.println("Total collisions for Single Hash table: " + hashTable.getCollisions() );
System.out.println("With a file of 200 inputs, that equals");
System.out.println("an average of " + ((double)(hashTable.getCollisions()) /200) + " collisions per attempt.");
System.out.println();
System.out.println("Total collisions for Double Hash table: " + doubleHashTable.getCollisions() );
System.out.println("With a file of 200 inputs, that equals");
System.out.println("an average of " + ((double)(doubleHashTable.getCollisions()) /200) + " collisions per attempt.");
} //end main
} //end code