Repository navigation
Expand file tree
/
Copy pathMyHashMap1.java
More file actions
153 lines (122 loc) · 3.96 KB
/
Copy pathMyHashMap1.java
File metadata and controls
153 lines (122 loc) · 3.96 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
package java_algorithms;
import java.util.*;
public class MyHashMap1 {
public static void main(String[] args) {
Map<String, Integer> myMap = new SimpleHashMap<>();
myMap.put("Test1", 1);
myMap.put("Test3", 3);
myMap.put("Test2", 2);
myMap.put("Test2", 5);
System.out.println(myMap);
}
public static class Node<K,V> implements Iterator {
private K key;
private V value;
private Node<K,V> next;
private int hashCode;
public Node(K key, V value, int hashCode, Node<K, V> next) {
this.key = key;
this.value = value;
this.next = next;
this.hashCode = hashCode;
}
public K getKey() {
return key;
}
public V getValue() {
return value;
}
public void setValue(V value) {
this.value = value;
}
@Override
public String toString() {
return key + "=" + value;
}
@Override
public boolean hasNext() {
return next != null;
}
@Override
public Object next() {
return next;
}
public Node<K, V> getNext() {
return next;
}
}
public static class SimpleHashMap<K, V> extends AbstractMap<K, V> {
private static final int SIZE = 16;
private Node<K,V>[] buckets = new Node[SIZE];
@Override
public Set<Entry<K, V>> entrySet() {
return null;
}
@Override
public V put(K key, V value) {
int hashCode = hash(key);
int index = index(hashCode);
if (buckets[index] == null) {
buckets[index] = new Node<K, V>(key, value, hashCode, null);
} else {
Node<K,V> curBacket = buckets[index];
while (curBacket.hasNext()) {
K curKey = curBacket.getKey();
if ( (key == null && curKey == null) || (key != null && key.equals(curKey))) {
V oldValue = curBacket.getValue();
curBacket.setValue(value);
return oldValue;
}
curBacket = curBacket.next;
}
if (curBacket.next == null) {
if (key.equals(curBacket.getKey())) {
V oldValue = curBacket.getValue();
curBacket.setValue(value);
return oldValue;
} else {
curBacket.next = new Node<K, V>(key, value, hashCode, null);
}
}
}
return null;
}
@Override
public V get(Object key) {
return super.get(key);
}
public int hash(Object key) {
return key == null ? 0 : key.hashCode();
}
public int index(int hashCode) {
return Math.abs(hashCode) % SIZE;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("{");
boolean first = true;
for (Node<K, V> bucket: buckets) {
if (bucket != null) {
if (!first) {
sb.append(",");
}
first = false;
sb.append(" [");
sb.append(bucket);
Node<K, V> curNode = bucket;
while (curNode.hasNext()) {
sb.append(", ");
sb.append(curNode.getNext().toString());
curNode = bucket.getNext();
}
sb.append("]");
} else {
sb.append(", null");
}
}
sb.append("}");
return sb.toString();
}
}
}