-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyHashTable.java
More file actions
174 lines (139 loc) · 3.98 KB
/
MyHashTable.java
File metadata and controls
174 lines (139 loc) · 3.98 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/* TCSS 305 - Compressed Literature 2
* MyHashTable Class
*/
import java.util.LinkedList;
import java.util.List;
/**
* A hash table.
*
* @author Rylie Nelson
*
* @param <K> The type of the Keys of this map.
* @param <V> The type of the Values of this map.
*/
public class MyHashTable<K, V> {
/**
* The "buckets" that entries are hashed into.
*/
private MyBucket<K, V>[] buckets;
/**
* The number of entries that this MyHashTable contains.
*/
private int entries;
/**
* Constructs a new MyHashTable with a given number of buckets.
*
* @param capacity The number of buckets.
*/
public MyHashTable(int capacity) {
buckets = new MyBucket[capacity];
for (int i = 0; i < capacity; i++) {
buckets[i] = new MyBucket<K, V>();
}
}
/**
* Puts an entry into this MyHashTable. If the value already exists for a given key, we replace it.
*
* @param searchKey The key of the entry we are putting in.
* @param newValue The value of the entry we are putting in.
*/
public void put(K searchKey, V newValue) {
int hashValue = hash(searchKey);
List<BucketNode<K, V>> theBucket = buckets[hashValue].bucketlist;
if (theBucket.isEmpty()) {
theBucket.add(new BucketNode<K, V>(searchKey, newValue));
entries++;
} else {
boolean replaced = false;
for (int i = 0; i < theBucket.size() || replaced; i++) {
if (theBucket.get(i).key.equals(searchKey)) {
theBucket.get(i).value = newValue;
replaced = true;
}
}
if (!replaced) {
theBucket.add(new BucketNode<K, V>(searchKey, newValue));
entries++;
}
}
}
/**
* Retrieves a value, given a key.
*
* @param searchKey The key we are using to search.
* @return The value associated with the key.
*/
public V get(K searchKey) {
int hashValue = hash(searchKey);
for (int i = 0; i < buckets[hashValue].bucketlist.size(); i++) {
if (buckets[hashValue].bucketlist.get(i).key.equals(searchKey)) {
return buckets[hashValue].bucketlist.get(i).value;
}
}
return null; //if we reach this point nothing was found, return null.
}
/**
* Prints out some interesting information about this MyHashTable.
*/
public void stats() {
System.out.println("Hash Table Stats");
System.out.println("===================");
System.out.println("Number of Entries: " + entries);
System.out.println("Number of Buckets: " + buckets.length);
int[] histogram = new int[10];
for (int i = 0; i < buckets.length; i++) {
histogram[buckets[i].bucketlist.size()]++;
}
System.out.print("Histogram of Bucket Sizes: [" + histogram[0]);
for (int i = 1; i < histogram.length; i++) {
System.out.print(" ," + histogram[i]);
}
System.out.println("]");
System.out.println("Fill Percentage: " + (((double) (buckets.length - histogram[0]) / buckets.length) * 100) + "%");
System.out.println("Average Non-Empty Bucket: " + entries / ((double) (buckets.length - histogram[0])));
}
/**
* Hashes a key so that it can be placed into a bucket.
*
* @param key The key to be hashed.
* @return The hash of the key.
*/
private int hash(K key) {
return key.hashCode() % (buckets.length / 2) + (buckets.length / 2);
}
public String toString() {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < buckets.length; i++) {
sb.append(buckets[i].bucketlist);
sb.append("\n");
}
return sb.toString();
}
/**
* Represents a bucket of the MyHashTable.
*
* @author Rylie Nelson
*/
private class MyBucket<K, V> {
private List<BucketNode<K, V>> bucketlist;
private MyBucket() {
bucketlist = new LinkedList<BucketNode<K, V>>();
}
public String toString() {
return bucketlist.toString();
}
}
private class BucketNode<K, V> {
private K key;
private V value;
private BucketNode(K thekey, V thevalue) {
key = thekey;
value = thevalue;
}
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("(" + key + ", " + value + ")");
return sb.toString();
}
}
}