-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathHashTableMap.java
More file actions
158 lines (132 loc) · 4.4 KB
/
Copy pathHashTableMap.java
File metadata and controls
158 lines (132 loc) · 4.4 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
import java.util.LinkedList;
import java.util.NoSuchElementException;
public class HashTableMap<KeyType, ValueType> implements MapADT<KeyType, ValueType> {
private int capacity;
private int size;
private LinkedList<LinkedNode<KeyType, ValueType>>[] keyValuePairs;
public HashTableMap(int capacity) {
this.capacity = capacity;
keyValuePairs = (LinkedList<LinkedNode<KeyType, ValueType>>[]) new LinkedList[capacity];
}
public HashTableMap() {
this.capacity = 10;
keyValuePairs = (LinkedList<LinkedNode<KeyType, ValueType>>[]) new LinkedList[10];
}
@Override
public boolean put(KeyType key, ValueType value) {
LinkedNode<KeyType, ValueType> pair = new LinkedNode<KeyType, ValueType>(key, value);
LinkedList<LinkedNode<KeyType, ValueType>> chain = new LinkedList();
if ((this.size / this.capacity) >= 0.85) {
rehash();
}
for(int i = 0; i < chain.size(); i++) {
if (key == null || keyValuePairs[Math.abs(key.hashCode())% capacity].get(i).equals(pair)) {
return false;
}
}
if (keyValuePairs[Math.abs(key.hashCode()) % capacity] == null) {
chain.add(pair);
keyValuePairs[Math.abs(key.hashCode())% capacity] = chain;
this.size++;
return true;
}
else {
chain = keyValuePairs[Math.abs(key.hashCode())% capacity];
chain.add(pair);
keyValuePairs[Math.abs(key.hashCode())% capacity] = chain;
this.size++;
return true;
}
}
public LinkedList<LinkedNode<KeyType, ValueType>>[] rehash() {
this.capacity = this.capacity * 2;
LinkedList<LinkedNode<KeyType, ValueType>>[] rehashArray;
//LinkedList<LinkedNode<KeyType, ValueType>> newChain = new LinkedList();
rehashArray = new LinkedList[this.capacity];
LinkedNode<KeyType, ValueType> temp;
KeyType newHash;
for (int i = 0; i < this.capacity; i++) {
LinkedList<LinkedNode<KeyType, ValueType>> newChain = new LinkedList();
for (int j = 0; j < keyValuePairs[i].size(); j++) {
temp = keyValuePairs[i].remove(j);
newHash = temp.getKey();
if (rehashArray[Math.abs(newHash.hashCode()) % this.capacity] == null) {
newChain.add(temp);
rehashArray[Math.abs(newHash.hashCode()) % this.capacity] = newChain;
} else {
newChain = rehashArray[Math.abs(newHash.hashCode()) % this.capacity];
newChain.add(temp);
rehashArray[Math.abs(newHash.hashCode()) % this.capacity] = newChain;
}
}
}
return rehashArray;
}
@Override
public ValueType get(KeyType key) throws NoSuchElementException {
ValueType searchedValue = null;
for (int i = 0; i < keyValuePairs.length; i++) {
if (keyValuePairs[i] != null) {
for (int j = 0; j < keyValuePairs[i].size(); j++) {
if (keyValuePairs[i].get(j).getKey() == key) {
searchedValue = keyValuePairs[i].get(j).getValue();
}
else {
throw new NoSuchElementException("Not in HashTable");
}
}
}
}
return searchedValue;
}
public void increaseSize() {
this.size++;
}
@Override
public int size() {
return size();
}
@Override
public boolean containsKey(KeyType key) {
for (int i = 0; i < keyValuePairs.length; i++) {
if (keyValuePairs[i] != null) {
for (int j = 0; j < keyValuePairs[i].size(); j++) {
if (keyValuePairs[i].get(j).getKey() == key) {
return true;
}
}
}
}
return false;
}
@Override
public ValueType remove(KeyType key) {
ValueType toReturn = null;
for (int i = 0; i < keyValuePairs.length; i++) {
if (keyValuePairs[i] != null) {
for (int j = 0; j < keyValuePairs[i].size(); j++) {
if (keyValuePairs[i].get(j).getKey() == key) {
toReturn = keyValuePairs[i].get(j).getValue();
keyValuePairs[i].remove(j);
this.size--;
break;
} else {
return null;
}
}
}
}
return toReturn;
}
@Override
public void clear() {
for (int i = 0; i < keyValuePairs.length; i++) {
if (keyValuePairs[i] != null) {
for (int j = 0; j < keyValuePairs[i].size(); j++) {
keyValuePairs[i].remove(j);
}
}
}
this.size=0;
}
}