-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHashTableMap.java
More file actions
118 lines (106 loc) · 3.08 KB
/
HashTableMap.java
File metadata and controls
118 lines (106 loc) · 3.08 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
// --== CS400 File Header Information ==--
// Name: Zachary Austin
// Email: zaustin@wisc.edu
// Team: EB
// TA: Keren
// Lecturer: Florian Heimerl
// Notes to Grader: N/A
import java.util.LinkedList;
public class HashTableMap<KeyType, ValueType> implements MapADT<KeyType, ValueType> {
private int capacity;
private LinkedList<Node>[] hashTable = new LinkedList[capacity];
private LinkedList pairList = new LinkedList();
private int size;
private KeyType key;
private ValueType value;
public HashTableMap(int capacity) {
this.capacity = capacity;
this.hashTable = new LinkedList[capacity];
for(int i = 0; i < capacity; i++) {
hashTable[i] = pairList;
}
this.size = 0;
}
public HashTableMap() {
this.capacity = 10; // with default capacity = 10
this.hashTable = new LinkedList[capacity];
for(int i = 0; i < capacity; i++) {
hashTable[i] = pairList;
}
this.size = 0;
}
public boolean put(KeyType key, ValueType value) {
for(int i = 0; i < hashTable.length; i++) {
if(hashTable[i] != null) {
for(int j = 0; j < hashTable[i].size(); j++) {
if(key.equals(hashTable[i].get(j).key)) {
return false;
}
}
}
}
Node newNode = new Node(key, value);
hashTable[Math.abs(key.hashCode() % capacity)].add(newNode);
size++;
if(((float)size / (float)capacity) >= 0.8) {
doubleAndRehash();
}
return true;
}
private void doubleAndRehash() {
LinkedList<Node>[] newHashTable = new LinkedList[2*capacity];
for(int i = 0; i < (2*capacity); i++) {
newHashTable[i] = pairList;
}
for(int i = 0; i < capacity; i++) {
newHashTable[i] = hashTable[i];
}
hashTable = newHashTable;
capacity *= 2;
}
public ValueType remove(KeyType key) {
for(int i = 0; i < hashTable[Math.abs(key.hashCode() % capacity)].size(); i++) {
if(key.equals(hashTable[Math.abs(key.hashCode() % capacity)].get(i).key)) {
Node removeNode = new Node(null, null);
removeNode = hashTable[Math.abs(key.hashCode() % capacity)].get(i);
hashTable[Math.abs(key.hashCode() % capacity)].remove(i);
return (ValueType) removeNode.value;
}
}
return null;
}
public int size() {
return size;
}
public void clear() {
for(int i = 0; i < capacity; i++) {
for(int j = 0; j < hashTable[i].size(); j++) {
hashTable[i].remove(j);
}
}
size = 0;
}
public ValueType get(KeyType key) throws java.util.NoSuchElementException {
for(int i = 0; i < hashTable.length; i++) {
if(hashTable[i] != null) {
for(int j = 0; j < hashTable[i].size(); j++) {
if(key.equals(hashTable[i].get(j).key)) {
return (ValueType)hashTable[i].get(j).value;
}
}
}
}
throw new java.util.NoSuchElementException("No key-value pair with that key " +
"exists in the hash table.");
}
public boolean containsKey(KeyType key) {
for(int i = 0; i < hashTable.length; i++) {
for(int j = 0; j < hashTable[i].size(); j++) {
if(key.equals(hashTable[i].get(j).key)) {
return true;
}
}
}
return false;
}
}